TAGS :Viewed: 6 - Published at: a few seconds ago

[ Problem with in_array and multidimensional array / object ]

Here is the $haystack array:

Array
(
    [438] => stdClass Object
        (
            [w] => 438
            [x] => 0
            [y] => 438
            [z] => 23
        )

    [4960] => stdClass Object
        (
            [w] => 4960
            [x] => 0
            [y] => 4960
            [z] => 37
        )

)

Why does this not work? How can I tell $needle is in $haystack. I am getting this error stdClass could not be converted to int.

$needle = 438;
if(in_array($needle,$haystack)){
  echo "yes";
}else{
  echo "no";
}

Answer 1


You are looking for the values and not for the keys. It works like this:

if ($haystack[$needle]){
   ...
}

Answer 2


Array Key Exists

Looks like the right way to go about this is to use the array_key_exists function.

As john and andreas have pointed out, you are looking for a key, not for a value. in_array searches array values.

<?php 
if(array_key_exists(438, $array)) { //found
  echo "yes";
}else{
  echo "no";
}

Documentation here: http://php.net/manual/en/function.array-key-exists.php

Answer 3


The function in_array checks that the given needle is equal to one of the values in the array, not one of the keys in the array.

You are essentially doing:

if (438 == stdClass Object(
            [w] => 438
            [x] => 0
            [y] => 438
            [z] => 23
        )
    ||
    438 ==  stdClass Object(
            [w] => 4960
            [x] => 0
            [y] => 4960
            [z] => 37
        )
   ) {
  echo "yes";
}
else {
  echo "no";
}

What you should use is:

$needle = 438;
if (array_key_exists($needle, $haystack)) {
  echo 'yes';
}
else {
  echo 'no';
}

Answer 4


first you need to convert your haystack to multidimensional array

Function to Convert stdClass Objects to Multidimensional Arrays

<?php

    function objectToArray($d) {
        if (is_object($d)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $d = get_object_vars($d);
        }

        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map(__FUNCTION__, $d);
        }
        else {
            // Return array
            return $d;
        }
    }

?>

http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/

You can get returned array, which will be perfect array ready to search

function in_multiarray($elem, $array)
{
    $top = sizeof($array) - 1;
    $bottom = 0;
    while($bottom <= $top)
    {
        if($array[$bottom] == $elem)
            return true;
        else 
            if(is_array($array[$bottom]))
                if(in_multiarray($elem, ($array[$bottom])))
                    return true;

        $bottom++;
    }        
    return false;
}