0

The return true; is not ending the current function loop! This is so basic but I don't find why break is not working!

I try this piece of code:

function findKey($array, $keySearch) {
foreach ($array as $key => $item){
    echo "$key == $keySearch";
    if ("$key" == "$keySearch"){
        echo 'yes, it exists';
        return true;
    }
    else 
    {
        if (is_array($item))
            findKey($item, $keySearch);
    }
}
return false;
}

The return true; never break as it let it go after the yes, it exists is echoed!

Solved

The code itself from an accepted solution here was wrong. It has been corrected. Also, below you will find the right answer.

Community
  • 1
  • 1
Vincent
  • 117
  • 8
  • 3
    `return` will immediately end the execution of a function and return whatever it is that you're returning. – Andrei Sep 28 '15 at 15:28
  • It's what I'm use to see, but in this case it's not working! – Vincent Sep 28 '15 at 15:29
  • 2
    the break statement is never executed. execution returns when it find the return satement. – Gautam Sep 28 '15 at 15:29
  • It break the current foreach but still resend the function in the else statement!! – Vincent Sep 28 '15 at 15:32
  • 2
    Recursive weirdness.... you're not doing anything with the return value from the recursive call to findKey()..... `if (is_array($item)) return findKey($item, $keySearch);` perhaps? – Mark Baker Sep 28 '15 at 15:33
  • @MarkBaker I get that piece of code from another accepted [post here](http://stackoverflow.com/questions/19420715/check-if-specific-array-key-exists-in-multidimensional-array-php/19421079#19421079). It's probably not a huge issue, but if my array became very large, it will probably affect performance! – Vincent Sep 28 '15 at 15:38
  • Even though ...it's just wrong ;-) – VolkerK Sep 28 '15 at 15:44
  • Thank you for all your fast answers! I will try another function as this one is not that good on performance! – Vincent Sep 28 '15 at 16:05
  • Try the one [that has been upvoted](http://stackoverflow.com/a/19420866/4833), not the accepted one. – VolkerK Sep 28 '15 at 16:09
  • @VolkerK it's exactly the one I choose! Thank you – Vincent Sep 28 '15 at 16:12
  • There is no `break` statement here. – Lightness Races in Orbit Sep 28 '15 at 16:21

1 Answers1

2

You've jumped to a conclusion. In fact, your return true is assuredly ending the function immediately (including breaking out of the loop), but it only ends the current invocation of the function. Your solution is recursive, and has a bug in the else clause, whereby the result of a recursive call to findKey is completely ignored. As such, whatever happens inside that invocation has no effect.

You're seeing the echo from your outer function call.

I imagine that what you really intended to write was this:

function findKey($array, $keySearch)
{
   foreach ($array as $key => $item) {
      echo "$key == $keySearch";
      if ($key == $keySearch) {
         echo 'yes, it exists';
         return true;
      }
      else {
         if (is_array($item) && findKey($item, $keySearch))
            return true;
      }
   }

   return false;
}

I have made the same change to the original answer that you'd found.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thank you. You are right, but it this specific case, it's causing a problem and also affecting performance. I tested both solutions, the first one took 0 sec the other one +/- 0.001 sec, in my situation it was not the best. Do you have any idea how to stop it recursively? – Vincent Sep 28 '15 at 16:29