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 3843698
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:58:30+00:00 2026-05-19T15:58:30+00:00

What is the cleanest way to pass the result of this recursive function back

  • 0

What is the cleanest way to pass the result of this recursive function back out?

function recursion_trigger($input, $count = 0){

        if(!empty($input)){
                array_pop($input);
                $count++;
                if(!empty($input)){
                recursion_trigger($input,$count);
                }
        }

echo $count;
return $count;


}

At the moment it is returning the top most call which is of course one.

/////// as an additional question this is the full function can you use tail recursion here? The output is an array that I am constructing as I pass through the values.

<?php    
//Page best viewed from page source 

 //Takes an array of objects that have ID and Parent and organizes the tree into an array representing a set of objectID's by depth 

 // poor spelling ahead =P

function level_keys($array,$depth=-1,$level=0,$output=null){

// initialize the functions parameters run once at start and not in subsequent self calls

if($level == 0 && $depth != 0){
    $output[][]=0;
    $level++;
        foreach($array as $key=>$node){
            if($node->parent==0){
                $output[$level][] = $node->id;
                unset($array[$key]);
            }
        }
            unset($key); unset($node);
$level++;
$depth--;

}

// set recursion loop and run main part of function

if (  !empty($array) && $depth != 0){

    echo 'depth:'.$depth."\n";

    foreach($output[$level-1] as $parent){  
        foreach($array as $key=> $child){
            if( $parent == $child->parent){
            $output[$level][] = $child->id;
            unset($array[$key]);
            }
        }
    }
        unset($id); unset($parent); unset($key); unset($child);
$depth--;
$level++;
        if(!empty($array) && $depth !=0 ){
            // make sure to pass the output back out to the top most level
            $output = level_keys($array,$depth,$level,$output,$depth_at);
        }
}

return $output;

}
?>
  • 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-05-19T15:58:31+00:00Added an answer on May 19, 2026 at 3:58 pm

    I guess what you really need is not to count the number of elements in an array.

    When you do recursive functions like this one, it is good for performance if they are tail-recursive (in fact, I’m not sure php has this optimisation, I hope so). Here you have $count that can serve as accumulator but don’t use it.

    function recursion_trigger ($input, $count = 0)
    {
      if (!empty($input)) {
        array_pop($input);
        return recursion_trigger($input, $count + 1);
      }
      return $count;
    }
    

    This way it works and is tail recursive :-).

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

Sidebar

Related Questions

Looking for suggestions on cleanest way to get the return value and the result
In SQL Server what is the simplest/cleanest way to make a datetime representing the
I am looking for the cleanest way. I am tempted to use delegates not
What is the easiest/simplest/cleanest way to: Read an xml file from the filesystem Validate
What is the best/cleanest/easiest way to maintain the edit history of records in Rails?
What is the best (cleanest, most efficient) way to write saturating addition in C?
I'm writing a filter function to return the specific type specified out of a
What is the cleanest way to recursively search for files using C++ and MFC?
Trying to add email notification to my app in the cleanest way possible. When
Is there a way to easily create Java Input/Output streams for unnamed pipes created

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.