user3350731
user3350731

Reputation: 972

PHP - Compare 2 different multidimensional arrays

I have this $firstarray :

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

And here's the $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
        )

)

I need to compare if the $key of the $firstarray equals to $value[] of the $secondarray, here's what I tried so far and didn't work :

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

Upvotes: 0

Views: 63

Answers (3)

lmanohara
lmanohara

Reputation: 77

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


}

Upvotes: 0

Eddie
Eddie

Reputation: 26844

How about this?

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

Upvotes: 1

Satish Sharma
Satish Sharma

Reputation: 9635

try this

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

Upvotes: 1

Related Questions