2

Not entirely sure how to adequately title this problem, but it entails a need to loop through any array nested within array, of which may also be an element of any other array - and so forth. Initially, I thought it was required for one to tag which arrays haven't yet been looped and which have, to loop through the "base" array completely (albeit it was learned this isn't needed and that PHP somehow does this arbitrarily). The problem seems a little peculiar - the function will find the value nested in the array anywhere if the conditional claus for testing if the value isn't found is omitted, and vice versa. Anyway, the function is as followed:

function loop($arr, $find) {
  for($i=0;$i<count($arr);$i++) {
    if($arr[$i] == $find) {
      print "Found $find";
      return true;
    } else {
      if(is_array($arr[$i])) {
         $this->loop($arr[$i], $find);
      } else {
         print "Couldn't find $find";
         return false;
      }
    }
   }
 }
user784446
  • 489
  • 1
  • 9
  • 22
  • What is the question, please? – shadyyx May 04 '12 at 11:50
  • @Tibor, using `foreach` is slower than using `for` which is irrelevant with small arrays but with multilevel arrays (and especially big) there will be nice speed difference... – shadyyx May 04 '12 at 11:51
  • Create a stack that holds the position of the parent array, loop through the child and if you encounter a child within the child then push your current position onto the stack and start looping through the grandchild, toerhwsie if you finish the child then pop the stack and continue looping through the parent from the point you got off the stack – Dan May 04 '12 at 11:51
  • @shadyyx: Is there really such a difference? I didn't check but I generally I don't see why would there be one. Do you have any reference on this or have you run any tests? –  May 04 '12 at 12:03
  • @Tibor Sorry, my bad. I remebered it wrong. I was reading a benchmark here at SE (http://stackoverflow.com/questions/3430194/performance-of-for-vs-foreach-in-php) and remembered it wrong - it seems that `foreach` is even slightly quicker than `for`. Please, accept my apology. But this test is showing different results over time: http://codepad.org/STW1Jd4j – shadyyx May 04 '12 at 12:38
  • No problem, thank you for verifying :) –  May 04 '12 at 12:39
  • @Tibor - pls check my comment above again, I've added a small benchmark test... – shadyyx May 04 '12 at 12:41
  • Apologies for the delayed response, but I've kinda solved the problem by using two variables external to the local environment of the function - one retains the current value of $i, and the second retains the total length of the input array, including all nested sub-arrays. The final conditional clause tests if the two values are equal, to which it then outputs a notification. The problem however, albeit minor, is that it will sometimes output the notification even after it's output a found notification even if $find is found -- I assume that this is due to the way PHP processes this. – user784446 May 04 '12 at 22:04
  • So, what I'm wondering is, how could I solve this without iterating throughout the entire array and all nested sub-arrays to determine the total length of the array -- ? Thanks! Oh, and for clarification: when referring to "the way PHP processes this", I refer to what appears to be PHP resuming the previous array after it's looped a nested array but which finds that the iterative value is equivalent to the current length of the array. – user784446 May 04 '12 at 22:04

4 Answers4

1

Perhaps you should change your code to something like:

var $found = false;
function loop($arr, $find) {
  foreach($arr as $k=>$v){
    if($find==$v){
      $this->found = true;
    }elseif(is_array($v)){
      $this->loop($v, $find);
    }
  }
  return $this->found;
}
Bloafer
  • 1,324
  • 7
  • 12
0

Try this: PHP foreach loop through multidimensional array

Community
  • 1
  • 1
Sliq
  • 15,937
  • 27
  • 110
  • 143
0

This has been working for me for a while.

function array_search_key( $needle_key, $array ) {
  foreach($array AS $key=>$value){
    if($key == $needle_key) return $value;
    if(is_array($value)){
      if( ($result = array_search_key($needle_key,$value)) !== false)
        return $result;
    }
  }
  return false;
}
Matthew Nie
  • 629
  • 5
  • 10
0

OK, what about a slight modification:

function loop($arr, $find) {
    for($i=0;$i<count($arr);$i++) {
        if(is_array($arr[$i])) {
            $this->loop($arr[$i], $find);
        } else {
            if($arr[$i] == $find) {
                print "Found $find";
                return true;
            }
        }
    }
    return false;
}

Hmm?

shadyyx
  • 15,825
  • 6
  • 60
  • 95