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;
      }
    }
   }
 }
9
  • What is the question, please? Commented May 4, 2012 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... Commented May 4, 2012 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 Commented May 4, 2012 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? Commented May 4, 2012 at 12:03
  • @Tibor Sorry, my bad. I remebered it wrong. I was reading a benchmark here at SE (stackoverflow.com/questions/3430194/…) 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: codepad.org/STW1Jd4j Commented May 4, 2012 at 12:38

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

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?

Comments

0

Try this: PHP foreach loop through multidimensional array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.