Searching multidimensional arrays I have been trying to parse results and make sure when I am doing so that I don't repeat the same data. I tried to use a few different options built into php with no luck. I did find an example of a recursive array search that seems to work but it's very intensive and adds a lot of time to the script.

What I'm needing: Does anyone know a better way to handle this without changing the array that I supply to it so something built-in like in_array or array_search?

Array example:

array (size=3)
  0 =&gt; 
    array (size=3)
      'author' =&gt; string 'Jim Beam' (length=8)
      'id' =&gt; string '1' (length=1)
      'md5' =&gt; string 'f2ebf4d4f333c31ef1491a377edf2cc4' (length=32)
  1 =&gt; 
    array (size=3)
      'author' =&gt; string 'Jack Daniels' (length=12)
      'id' =&gt; string '2' (length=1)
      'md5' =&gt; string 'd1839707c130497bfd569c77f97ccac7' (length=32)
  2 =&gt; 
    array (size=3)
      'author' =&gt; string 'Jose Cuervo' (length=11)
      'id' =&gt; string '3' (length=1)
      'md5' =&gt; string '64e989b4330cc03dea7fdf6bfe10dda1' (length=32)


Code example:

function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=&gt;$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) &amp;&amp; recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}

$agentArray = array(
    array('author'=&gt;'Jim Beam','id'=&gt;'1','md5'=&gt;'f2ebf4d4f333c31ef1491a377edf2cc4'),
    array('author'=&gt;'Jack Daniels','id'=&gt;'2','md5'=&gt;'d1839707c130497bfd569c77f97ccac7'),
    array('author'=&gt;'Jose Cuervo','id'=&gt;'3','md5'=&gt;'64e989b4330cc03dea7fdf6bfe10dda1')
);

$fakeMD5 = '84d7dc19766c446f5e4084e8fce87f82'; //StackOverflow MD5
$realMD5 = 'd1839707c130497bfd569c77f97ccac7'; //Jack Daniels MD5

echo '&lt;b&gt;In_Array:&lt;/b&gt; &lt;br/&gt;';

$faketest = in_array($fakeMD5,$agentArray);
$realtest = in_array($realMD5,$agentArray);

var_dump($faketest,$realtest);

echo '&lt;b&gt;Search_Array:&lt;/b&gt; &lt;br/&gt;';

$faketest2 = array_search($fakeMD5,$agentArray);
$realtest2 = array_search($realMD5,$agentArray);

var_dump($faketest2,$realtest2);

echo '&lt;b&gt;Custom Recursive Array Seach Function:&lt;/b&gt; &lt;br/&gt;';

$faketest3 = recursive_array_search($fakeMD5,$agentArray);
$realtest3 = recursive_array_search($realMD5,$agentArray);

var_dump($faketest3,$realtest3);


Results:



In_Array:
Fake: boolean false
Real: boolean false

Search_Array:
Fake: boolean false
Real: boolean false

Custom Recursive Array Seach Function:
Fake: boolean false
Real: int 1
I have two comments.

First, what is the purpose of $current_key? Since you're not changing it, just use $key.

Second, by your usage, I'd say that

foreach ($haystack as $key =&gt; $item)
    if ($item["md5"] === $needle) return $key;
return false;


is quite enough. Of course, if the above was just an example and you really want to check all the values in (sub)arrays, then the above is O.K.

Of course, be careful when using this function, because key 0 may be interpreted as false.

A bit neater (but essentially the same) function was provided on Stack Overflow here. I like the additional $strict argument there (which you may or may not need).