PHP -比较两个不同的多维数组



我有这个$firstarray:

Array
(
    [1] => page->Accueil  // <--- This $key
    [2] => contact->Contact
    [3] => page->Page Test
    [4] => gallery->Test
    [6] => article->test
)

这是$secondarray:

Array
(
    [0] => Array
        (
            [0] => page 
            [1] => 1  //<--- With this $value
        )
    [1] => Array
        (
            [0] => contact
            [1] => 2
        )
    [2] => Array
        (
            [0] => page
            [1] => 3
        )
    [3] => Array
        (
            [0] => gallery
            [1] => 4
        )
    [4] => Array
        (
            [0] => article
            [1] => 6
        )
)

我需要比较$firstarray$key是否等于$secondarray$value[],这是我到目前为止尝试的,没有工作:

foreach ($firstarray as $key => $value) {
if (array_key_exists($key, $secondarray)) {
echo "Ok";
}
}

try this

foreach ($firstarray as $key => $value) 
{
   foreach ($secondary as $key2 => $value2) 
   {
        if(in_array($key, $value2)
        {
            echo "Ok"; 
            break;  
        } 
   }
}

这个怎么样?

foreach ($secondarray as $key => $value) {
    if (array_key_exists($value[1], $firstarray)) {
        echo "Ok";
    }
}
<?php
$testArray = array("page"=>"Accueil",
    "contact"=>"Contact",
    "page"=>"Page Test",
    "gallery"=>"Test",
    "article"=>"test");
$testArray2 = array(array('page' => '1'),array( 'contact'=> '2'));
foreach ($testArray as $key => $value) {
    for($i =0; $i < sizeof($testArray2); $i++){
        if(array_key_exists($key, $testArray2[$i])){
            echo "ok"
        }
    }

}

相关内容

  • 没有找到相关文章

最新更新