up vote 1 down vote favorite

hi,

I use in_array() to check whether a value exists in an array like below,

$a = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $a)) 
{
    echo "Got Irix";
}

//print_r($a);

but what about an multidimensional array (below) - how can I check that value whether it exists in the multi-array?

$b = array(array("Mac", "NT"), array("Irix", "Linux"));

print_r($b);

or I shouldn't be using in_array() when comes to the multidimensional array?

link|flag

3 Answers

up vote 3 down vote accepted

in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you:

function in_array_r($needle, $haystack) {
    $found = false;

    foreach ($haystack as $item) {
        if ($item == $needle) {
            $found = true;
            break;
        } elseif (is_array($item)) {
            $found = in_array_r($needle, $item);
        }
    }

    return $found;
}

Usage:

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
if (in_array_r("Irix", $b)) {
    echo 'found';
} else {
    echo 'not found';
}
link|flag
thank you. the function is elegant! love it! thanks. how can i know it returns true or false as my screen doest show anything when I run your function? thanks. – lauthiamkok Nov 8 at 22:00
@lauthiamkok: Check out my updated usage example ;) – elusive Nov 8 at 22:01
got it! thanks so much! :-))) – lauthiamkok Nov 8 at 22:11
@lauthiamkok: You're welcome! – elusive Nov 8 at 22:15
up vote 0 down vote

This is the first function of this type that I found in the php manual for in_array. Functions in the comment sections aren't always the best but if it doesn't do the trick you can look in there too :)

<?php
function in_multiarray($elem, $array)
    {
        // if the $array is an array or is an object
         if( is_array( $array ) || is_object( $array ) )
         {
             // if $elem is in $array object
             if( is_object( $array ) )
             {
                 $temp_array = get_object_vars( $array );
                 if( in_array( $elem, $temp_array ) )
                     return TRUE;
             }

             // if $elem is in $array return true
             if( is_array( $array ) && in_array( $elem, $array ) )
                 return TRUE;


             // if $elem isn't in $array, then check foreach element
             foreach( $array as $array_element )
             {
                 // if $array_element is an array or is an object call the in_multiarray function to this element
                 // if in_multiarray returns TRUE, than return is in array, else check next element
                 if( ( is_array( $array_element ) || is_object( $array_element ) ) && $this->in_multiarray( $elem, $array_element ) )
                 {
                     return TRUE;
                     exit;
                 }
             }
         }

         // if isn't in array return FALSE
         return FALSE;
    }
?>
link|flag
elusive's solution is better since it's just for arrays – Gazillion Nov 8 at 21:46
up vote 0 down vote

This will do it:

foreach($b as $value)
{
    if(in_array("Irix", $value))
    {
        echo "Got Irix";
    }
}

in_array only operates on a one dimensional array, so you need to loop over each sub array and run in_array on each.

As others have noted, this will only for for a 2-dimensional array. If you have more nested arrays, a recursive version would be better. See the other answers for examples of that.

link|flag
2  
However, this will only work in one dimension. You'll have to create a recursive function in order to check each depth. – metrobalderas Nov 8 at 21:44
i ran the code but it has an error - Parse error: parse error in C:\wamp\www\000_TEST\php\php.in_array\index.php on line 21 - which is if(in_array("Irix", $value) thanks. – lauthiamkok Nov 8 at 21:51
@lauthiamkok: There is a ) missing at the end of the mentioned line. – elusive Nov 8 at 21:52
Thanks, I fixed my answer. That's what happens when I type too fast and don't reread my code. – Alan Geleynse Nov 8 at 21:53
lol i missed that too! thanks! – lauthiamkok Nov 8 at 21:55

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.