简体   繁体   中英

PHP in_array() unexpected result

This is the $feed_content array

Array
    (
        [0] => Array
            (
                [id] => 1
                [link] => http://www.dust-off.com/10-tips-on-how-to-love-your-portable-screens-keeping-them-healthy
    )
        [1] => Array
            (
                [id] => 2
                [link] => http://www.dust-off.com/are-you-the-resident-germaphobe-in-your-office
            )
     )

----Another array----------
This is the $arrFeeds array

Array
(
    [0] => Array
        (
            [title] => 10 Tips on How to Love Your Portable Screens (Keeping them Healthy)
            [link] => http://www.dust-off.com/10-tips-on-how-to-love-your-portable-screens-keeping-them-healthy
  )
   [1] => Array
        (
            [title] => Are You the Resident Germaphobe in Your Office?
            [link] => http://www.dust-off.com/are-you-the-resident-germaphobe-in-your-office
        )


)

Here's my code:

foreach( $arrFeeds as $key2 => $value2 )
    {
        $feed_content = $feed->get_feed_content( $value['id'] );

        if( !in_array( $value2['link'], $feed_content ) )
        {
            echo "not match!";
        }
    }

Question:
Why is it that the code always enters into the if statement even if $feed_content link value has the value of the $arrFeeds link? My expected result should be that my code would tell me if the $feed_content link value is not in the $arrFeeds. By the way, the $feed_content code returns an array that I specified above. What should be the problem with this one. Thanks in advance! :)

that's because the elements of your array called $feed_content are associative arrays also (with the id and link keys)

you are checking if the link (a string) is equal to any of the elements in the array (all of them arrays)

Edit:

To achieve what you want you can use a "hack". Instead of in_array you can use something like:

$search_key = array_search(array('id'=>true,'link'=>$value2['link']), $feed_content);//use true for the id, as the comparison won't be strict
if (false !== $search_key)//stict comparison to be sure that key 0 is taken into account
{
    echo 'match here';
}

this thing relies on the fact that you can use an array for the search needle for the array_search function and that the comparison won't be strict, so true will match any number (except 0, but I guess that you don't use 0 as an id)

this way the only field that will really matter is the one for the link

after that you need to use a strict comparison this time to be sure that if the found key is 0 you will use it

in_array doesn't search recursively. It sees $feed_content as

Array
(
    [0] => Array
    [1] => Array
)

Now you could extend your if clause to foreach through $feed_content :

$found = false;
foreach($feed_content as $feedArr)
{
    if(in_array($value2['link'], $feedArr))
    {
        $found = true;
    }
}
if(!$found) echo "not match!";
if( !in_array( $value2['link'], $feed_content ) )



if( !in_array( $value2['link'], array_values($feed_content)) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM