Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 taht seems to work but its very intesive 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 => 
    array (size=3)
      'author' => string 'Jim Beam' (length=8)
      'id' => string '1' (length=1)
      'md5' => string 'f2ebf4d4f333c31ef1491a377edf2cc4' (length=32)
  1 => 
    array (size=3)
      'author' => string 'Jack Daniels' (length=12)
      'id' => string '2' (length=1)
      'md5' => string 'd1839707c130497bfd569c77f97ccac7' (length=32)
  2 => 
    array (size=3)
      'author' => string 'Jose Cuervo' (length=11)
      'id' => string '3' (length=1)
      'md5' => string '64e989b4330cc03dea7fdf6bfe10dda1' (length=32)

CODE EXAMPLE:

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

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

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

echo '<b>In_Array:</b> <br/>';

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

var_dump($faketest,$realtest);

echo '<b>Search_Array:</b> <br/>';

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

var_dump($faketest2,$realtest2);

echo '<b>Custom Recursive Array Seach Function:</b> <br/>';

$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
share|improve this question
1  
Also for PHP 5.5+ there is a simpler option, array_search($value, array_column($array, $key)); see stackoverflow.com/a/24527099/207603 (there is also a link to a backport for < PHP 5.5) – Radu Maris Jan 22 '15 at 9:38

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 => $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).

share|improve this answer
    
Yeah thats not my function someone added that in a different thread but I think I overlooked the simplicity of my operation. I will use your example and see if its faster! – sin0cide Aug 23 '13 at 22:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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