Php 递归结果堆栈
将这个递归函数的结果传回最干净的方法是什么Php 递归结果堆栈,php,function,recursion,stack,Php,Function,Recursion,Stack,将这个递归函数的结果传回最干净的方法是什么 function recursion_trigger($input, $count = 0){ if(!empty($input)){ array_pop($input); $count++; if(!empty($input)){ recursion_trigger($input,$count);
function recursion_trigger($input, $count = 0){
if(!empty($input)){
array_pop($input);
$count++;
if(!empty($input)){
recursion_trigger($input,$count);
}
}
echo $count;
return $count;
}
目前,它返回的电话最多,当然是一个
///////另外一个问题是,这是完整的函数,您可以在这里使用尾部递归吗?输出是一个数组,我在通过这些值时正在构造该数组
<?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;
}
?>
您应该使用递归\u触发器的返回值更新$count
变量
if(!empty($input)){
$count = recursion_trigger($input,$count);
}
编辑:
希望以下内容能帮助您直观了解其工作原理:
recursion_trigger ( array("This", "Is", "A", "Sample"), 0)
recursion_trigger ( array("This", "Is", "A"), 1)
recursion_trigger ( array("This", "Is"), 2)
recursion_trigger ( array("This"), 3)
recursion_trigger ( array(), 4)
您应该使用递归\u触发器的返回值更新$count
变量
if(!empty($input)){
$count = recursion_trigger($input,$count);
}
编辑:
希望以下内容能帮助您直观了解其工作原理:
recursion_trigger ( array("This", "Is", "A", "Sample"), 0)
recursion_trigger ( array("This", "Is", "A"), 1)
recursion_trigger ( array("This", "Is"), 2)
recursion_trigger ( array("This"), 3)
recursion_trigger ( array(), 4)
您可能认为,$count
是持久的,而不是因为值调用。这个版本使用了引用,也可以使用
function recursion_trigger($input, &$count = 0){
if(!empty($input)){
array_pop($input);
$count++;
if(!empty($input)){
recursion_trigger($input,$count);
}
}
echo $count;
return $count;
}
您可能认为,$count
是持久的,而不是因为值调用。这个版本使用了引用,也可以使用
function recursion_trigger($input, &$count = 0){
if(!empty($input)){
array_pop($input);
$count++;
if(!empty($input)){
recursion_trigger($input,$count);
}
}
echo $count;
return $count;
}
我想你真正需要的不是计算数组中的元素数
当您使用像这样的递归函数时,如果它们是尾部递归的,则对性能有好处(事实上,我不确定php是否有这种优化,我希望如此)。这里有$count可以用作累加器,但不要使用它
function recursion_trigger ($input, $count = 0)
{
if (!empty($input)) {
array_pop($input);
return recursion_trigger($input, $count + 1);
}
return $count;
}
通过这种方式,它是尾部递归的:-)。我想您真正需要的是不计算数组中的元素数
当您使用像这样的递归函数时,如果它们是尾部递归的,则对性能有好处(事实上,我不确定php是否有这种优化,我希望如此)。这里有$count可以用作累加器,但不要使用它
function recursion_trigger ($input, $count = 0)
{
if (!empty($input)) {
array_pop($input);
return recursion_trigger($input, $count + 1);
}
return $count;
}
这样它就工作了,并且是尾部递归的:-)。谢谢你,就是这样。我理解它为什么工作,但我仍然很难想象它是如何工作的。谢谢你,就是这样。我理解它为什么工作,但我仍然很难想象它是如何工作的。我已经考虑过了,但这最终将成为Java中的一个函数。我只是想把一切都可视化,PHP是一种非常快速的语言。谢谢。我已经考虑过了,但这最终将成为Java中的一个函数。我只是想把一切都可视化,PHP是一种非常快速的语言。谢谢。哈哈,不,这是一个很好的方法来抽象出问题中不重要的部分。我还为这里发生的任何其他人找到了这个链接。我理解尾部递归的好处,我如何将它应用到我在上述问题中编辑的函数中。哈哈,不,这是一个很好的方法,可以抽象出问题中不重要的部分。我还为这里发生的任何其他人找到了这个链接。我理解尾部递归的好处,如何将其应用于我在上述问题中编辑的函数。