0

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

3 Answers 3

1

try this

foreach ($firstarray as $key => $value) 
{
   foreach ($secondary as $key2 => $value2) 
   {
        if(in_array($key, $value2)
        {
            echo "Ok"; 
            break;  
        } 
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

How about this?

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

Comments

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


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.