0

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! :)

5
  • By the way, the $feed_content code returns an array that I specified above. Commented Mar 14, 2012 at 15:36
  • 1
    Your expectation is wrong. You are looking into an array of arrays, not into an array of links. That's all. Create an array of links and then use in_array on that one (or use the recursive mode). Commented Mar 14, 2012 at 15:36
  • I don't believe in_array will search a multi-dimensional array in this manner. Commented Mar 14, 2012 at 15:38
  • @Cfreak: Do you have any idea how to check if the value of the array is existing on the other array (with a multi-dimensional array)? Commented Mar 14, 2012 at 15:45
  • @ElsonSolano I updated my answer and added a possible solution to your problem also.. you can see that it does what it should here: codepad.org/ZqGEJQAt (at least from what I understand..) Commented Mar 14, 2012 at 16:06

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

This is indeed a nasty hack. :-)
I wanted the code to be like if $arrFeeds didn't' find a link value in $feed_content link. Maybe it's just a matter of changing the if (false !== $search_key) statement in your code.
@ElsonSolano right, that is just a matter of how you will use this thing, the sample is just to show how it works; the main idea is that if the array_function returns a numeric value that is a key in the feed_content array
0

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

1 Comment

Do you know how would I check if that value exist in the other array? Thanks for your answer :)
0
if( !in_array( $value2['link'], $feed_content ) )



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

3 Comments

How does the array_values($feed_content) give you the values of the nested arrays? Won't it be the same array ($feed_content) again?
@Martin: Yes, that's what I think so too. that code still ain't working. Thanks for your answer though :)
Calling array_values on $feed_content will have absolutely no effect, it amounts to the same thing the OP is already doing. The array keys are already numeric and zero-indexed.

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.