Jump to content

Comparing Array


N-Bomb(Nerd)

Recommended Posts

Hi, I have two arrays ($StateList_1, $StateList_2) of states that I'm trying to compare.

 

The arrays look like this:

//$StateList_1
Array
(
    [illinois] => 97
    [Connecticut] => 88
    [Delaware] => 97
    [Montana] => 96
    [Georgia] => 82
    [Ohio] => 91
)


//$StateList_2
Array
(
    [illinois] => 97
    [Connecticut] => 88
    [Delaware] => 97
    [Montana] => 96
    [Georgia] => 82
    [Ohio] => 84
    [Texas] => 91
)

 

I'm trying to compare the two arrays using the state name (the keys of the arrays). The numbers serve a different purpose later on in my script, so just ignore those for now.

 

What I'm trying to do is compare $StateList_1 with $StateList_2. I've tried doing this:

 

foreach(array_keys($StateList_1) as $StateName)
{
    
    if(!in_array($StateName, array_keys($StateList_2)))
    {

        echo "Missing: " . $StateName . "<br>";

    }

}

 

However, that doesn't seem to be working so well.

 

Suggestions?

Link to comment
https://forums.phpfreaks.com/topic/201957-comparing-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.