Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:51:51+00:00 2026-06-05T09:51:51+00:00

Not sure what I’m doing wrong with this recursion function. I have an array

  • 0

Not sure what I’m doing wrong with this recursion function.
I have an array with a tree of a website and a page I’m looking for can be infinitely deep.
The function goes through all possibilities but sometimes doesn’t stop when it finds the right “page”.

The array

$haystack = array(
    array("id" => 1,"parent" => 0,"route" => "home","children" => array()),
    array("id" => 2,"parent" => 0,"route" => "news","children" => array()),
    array("id" => 3,"parent" => 0,"route" => "photography","children" => array(
                array("id" => 6,"parent" => 3,"route" => "photography/portraits","children" => array()),
                array("id" => 7,"parent" => 3,"route" => "photography/countries","children" => array()),
                array("id" => 8,"parent" => 3,"route" => "photography/landscapes","children" => array(
                                array("id" => 9,"parent" => 8,"route" => "photography/landscapes/city","children" => array()),
                                array("id" => 10,"parent" => 8,"route" => "photography/landscapes/wilderness","children" => array())
                            )
                )
        )
    ),
    array("id" => 4,"parent" => 0,"route" => "about","children" => array()),
    array("id" => 5,"parent" => 0,"route" => "contact","children" => array()),
);

The recursion function

function recurse($needle = -1, $haystack = NULL){
    $_tmp = array();

    foreach($haystack as $key => $item)
    {
        echo $needle ." === ". $item["id"] . "<br/>";

        if((string)$item["id"] === (string)$needle){
            echo "Found: " . $needle . "<br/><br/>";
            $_tmp = $item;
            break;
            //return $item;   <-- this doesn't work either
        } else {
            $_tmp = recurse($needle, $item["children"]);
        }
    }
    return $_tmp;
}

Test cases:

$test = recurse(3);
print_r($test);

$test = recurse(7);
print_r($test);

$test = recurse(9);
print_r($test);

Last test outputs:

9 === 1
9 === 2
9 === 4
9 === 7
9 === 8
9 === 11
9 === 12
9 === 13
9 === 14
9 === 15
9 === 3
9 === 9
Found: 9  <-- should stop here, but continues

9 === 5
9 === 6
Array
(
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:51:53+00:00Added an answer on June 5, 2026 at 9:51 am

    Here is a slightly modified recurse function that fixes the problem you have:

     function recurse($needle = -1, $haystack = NULL){
        static $_tmp = array();
    
        if (count($_tmp) == 0 && $haystack != NULL && count($haystack) > 0) {
           foreach($haystack as $key => $item) {
              if (count($_tmp) == 0) {
                  echo $needle ." === ". $item["id"] . "<br/>\n";
    
                  if((string)$item["id"] === (string)$needle){
                      echo "Found: " . $needle . "<br/>\n";
                      $_tmp = $item;
                      break;
                  } elseif (!empty($item["children"])) {
                      echo "calling ". $item["id"]. ".children <br/>\n";
                      $_tmp = recurse($needle, $item["children"]);
                  }
              }
           }
        }
        return $_tmp;
    }
    

    Basically it declares a static variable $_tmp that gets initialized only once and and then a check to process the loop only if $_tmp is empty makes sure to stop further processing once needle has been found.

    Online working demo of above code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure how to ask this or if it can be done. I have
Not sure this is possible but maybe you can help. I have a list,
Not sure what I'm doing wrong here. Just trying to make it a modal
Not sure if this is a basic question or not but I can't seem
not sure what is wrong with this jquery code: http://jsfiddle.net/aCABM/ I want to to
Not sure where to post this, but I can't find any information anywhere and
Not sure if anyone has experience this issue. I have a TextBox that I
Not sure what I'm doing wrong, but here is the code 1: var currDoc:NotesDocument
Not sure how to explain it and can't seem to figure out why this
Not sure if I'm doing this right: /(https?:\/\/\S+)(\s|(& nbsp;))?/g; This should match a URL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.