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

时间:2014-05-19 08:58:17

标签: php arrays multidimensional-array

我有$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
        )

)

我需要比较$key的{​​{1}}是否等于$firstarray的{​​{1}},这是我到目前为止尝试过的事情并没有&#39工作:

$value[]

3 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:1)

这个怎么样?

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

答案 2 :(得分:0)

<?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"
        }
    }


}