PHP递归不停止

PHP递归不停止,php,recursion,Php,Recursion,我不确定这个递归函数有什么问题。 我有一个带有网站树的数组,我正在寻找的页面可以无限深。 该函数会处理所有可能的情况,但有时在找到正确的“页面”时不会停止 阵列 $haystack = array( array("id" => 1,"parent" => 0,"route" => "home","children" => array()), array("id" => 2,"parent" => 0,"route" => "news","

我不确定这个递归函数有什么问题。 我有一个带有网站树的数组,我正在寻找的页面可以无限深。 该函数会处理所有可能的情况,但有时在找到正确的“页面”时不会停止

阵列

$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()),
);
递归函数

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;
}
最后测试输出:

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
(
)
9==1
9 === 2
9 === 4
9 === 7
9 === 8
9 === 11
9 === 12
9 === 13
9 === 14
9 === 15
9 === 3
9 === 9

发现:9返回,但在其他递归帧中继续。 例如,调用:1->2->3->4。
从4返回,但3(1->2->3)继续执行循环。

只有在您所在的级别上找不到任何东西时,才能进入阵列,如下所示:

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
        }   
    }   
    if (empty($_tmp))
        foreach($haystack as $key => $item)
        {   
            $_tmp = recurse($needle, $item["children"]);
        }   

    return $_tmp;
}
函数递归($needle=-1,$haystack=NULL){
$_tmp=array();
foreach($haystack作为$key=>$item)
{   
echo$pinder.“==”$item[“id”]。“
”; 如果((字符串)$item[“id”]===(字符串)$needle){ echo“找到:.$针。”

; $\u tmp=$项; 打破
//return$item;这里是一个稍微修改的递归函数,它修复了您的问题:

 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;
}
函数递归($needle=-1,$haystack=NULL){
静态$_tmp=array();
如果(计数($\u tmp)==0&&$haystack!=NULL&&count($haystack)>0){
foreach($haystack作为$key=>$item){
如果(计数($\u tmp)==0){
echo$needle.“==”$item[“id”]。“
\n”; 如果((字符串)$item[“id”]===(字符串)$needle){ echo“找到:“.$pinder.”
\n; $\u tmp=$项; 打破 }elseif(!empty($item[“children”])){ 回显“正在调用.$item[“id”]。.children
\n”; $_tmp=recurse($needle,$item[“children”]); } } } } 返回$\u tmp; }
基本上,它声明了一个静态变量
$\u tmp
,该变量只初始化一次,然后仅当
$\u tmp
为空时才进行检查,以确保在找到指针后停止进一步处理


您是否尝试过
返回$item;
而没有
中断;
?问题标题本不应该很有趣,但我一直在反复阅读并大笑。检查这个。这是为了嵌套注释,但它几乎是空值上的sameA foreach会抛出错误并杀死脚本是的,我知道,但我认为返回应该是空的算了,我试过“突破2”但我得到了一个错误。我不想让函数停止一切:PIt不是好的做法,但你可以通过引用传递一些最初设置为false的布尔变量,并在中断之前将其设置为true。在循环开始之前,检查它的值并返回它是否为true。我得到了值,但有没有办法停止所有进程?你的测试输出t表示它在找到指针后继续递归。如果我有一个巨大的数组,大约1000个项目,如果我在前10个项目中查找某个项目,它就没有继续的意义。另外,如果我可以在该点停止进程,那么我将返回值。你可以通过另一次静态va检查来提高它的效率变量在循环中的大小与我上次编辑时相同。请参见此处的演示:g ideone.com/wxtnb谢谢你,anubhava!工作:)
 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;
}