<i id='uypGf'><tr id='uypGf'><dt id='uypGf'><q id='uypGf'><span id='uypGf'><b id='uypGf'><form id='uypGf'><ins id='uypGf'></ins><ul id='uypGf'></ul><sub id='uypGf'></sub></form><legend id='uypGf'></legend><bdo id='uypGf'><pre id='uypGf'><center id='uypGf'></center></pre></bdo></b><th id='uypGf'></th></span></q></dt></tr></i><div id='uypGf'><tfoot id='uypGf'></tfoot><dl id='uypGf'><fieldset id='uypGf'></fieldset></dl></div>
  • <tfoot id='uypGf'></tfoot>

    <small id='uypGf'></small><noframes id='uypGf'>

        • <bdo id='uypGf'></bdo><ul id='uypGf'></ul>
        <legend id='uypGf'><style id='uypGf'><dir id='uypGf'><q id='uypGf'></q></dir></style></legend>

        PHP 不会中断递归 foreach 循环

        时间:2023-09-21

        <small id='9Rhhu'></small><noframes id='9Rhhu'>

      1. <legend id='9Rhhu'><style id='9Rhhu'><dir id='9Rhhu'><q id='9Rhhu'></q></dir></style></legend>

            <tfoot id='9Rhhu'></tfoot>

          • <i id='9Rhhu'><tr id='9Rhhu'><dt id='9Rhhu'><q id='9Rhhu'><span id='9Rhhu'><b id='9Rhhu'><form id='9Rhhu'><ins id='9Rhhu'></ins><ul id='9Rhhu'></ul><sub id='9Rhhu'></sub></form><legend id='9Rhhu'></legend><bdo id='9Rhhu'><pre id='9Rhhu'><center id='9Rhhu'></center></pre></bdo></b><th id='9Rhhu'></th></span></q></dt></tr></i><div id='9Rhhu'><tfoot id='9Rhhu'></tfoot><dl id='9Rhhu'><fieldset id='9Rhhu'></fieldset></dl></div>
            • <bdo id='9Rhhu'></bdo><ul id='9Rhhu'></ul>
                    <tbody id='9Rhhu'></tbody>

                  本文介绍了PHP 不会中断递归 foreach 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个像下面这样的递归函数.

                  I have a recursive function like below.

                  public function findnodeintree($cats,$cat_id)
                  {       
                      foreach($cats as $node)
                      {                   
                          if((int)$node['id'] == $cat_id)
                          {       
                              echo "finded";
                              $finded = $node;
                              break;
                          }
                          else
                          {
                              if(is_array($node) && array_key_exists('children', $node)){ 
                                  $this->findnodeintree($node['children'],$cat_id);
                              }
                          }           
                      }
                      return $finded;
                  }
                  

                  例如

                  $node =$this->findnodeintree($category_Array, 169);
                  

                  它给了我

                  "founded"
                  

                  遇到 PHP 错误

                  Severity: Notice
                  
                  Message: Undefined variable: finded
                  

                  数组结构就像

                      [0] => Array
                      (
                          [id] => 0
                          [name] => MAIN CATEGORY
                          [depth] => 0
                          [lft] => 1
                          [rgt] => 296
                          [children] => Array
                              (
                                  [0] => Array
                                      (
                                          [id] => 167
                                          [name] =>  CAT 0
                                          [depth] => 1
                                          [lft] => 2
                                          [rgt] => 17
                                          [children] => Array
                                              (
                                                  [0] => Array
                                                      (
                                                          [id] => 169
                                                          [name] =>   CAT 1
                                                          [depth] => 2
                                                          [lft] => 3
                                                          [rgt] => 4
                                                      )
                  
                                                  [1] => Array
                                                      (
                                                          [id] => 170
                                                          [name] =>   CAT 2
                                                          [depth] => 2
                                                          [lft] => 5
                                                          [rgt] => 10
                                                          [children] => Array
                                                              (
                                                                  [0] => Array
                                                                      (
                                                                          [id] => 171
                                                                          [name] =>    CAT 5
                                                                          [depth] => 3
                                                                          [lft] => 6
                                                                          [rgt] => 7
                                                                      )
                  
                                                                  [1] => Array
                                                                      (
                                                                          [id] => 172
                                                                          [name] =>    CAT 3
                                                                          [depth] => 3
                                                                          [lft] => 8
                                                                          [rgt] => 9
                                                                      )
                  
                                                              )
                  
                                                      )
                  

                  推荐答案

                  要从递归中获得正确的值,您的递归调用不能丢弃返回值.而且由于您想在遇到命中后立即返回递归树,并实际返回匹配的节点,因此您也必须在此时中断循环.

                  To get the right value from recursion, your recursion call must not discard the return value. And since you want to walk back up the recursion tree as soon as you get a hit, and actually return the matching node, you have to break your loop at that point too.

                  否则后续的递归调用会覆盖您的变量并返回错误的节点、falsenull.

                  Otherwise subsequent recursion calls overwrite your variable and the wrong node, false, or null is returned.

                  这应该是有效的:

                  public function findnodeintree($cats,$cat_id)
                  {       
                      foreach($cats as $node)
                      {                   
                          if((int)$node['id'] == $cat_id){       
                              return $node;
                          }
                          elseif(array_key_exists('children', $node)) {
                              $r = $this->findnodeintree($node['children'], $cat_id);
                              if($r !== null){
                                  return $r;
                              }
                          }           
                      }
                      return null;
                  }
                  

                  注意:我删除了 is_array 因为那时 $node 必须是一个数组或在第一个分支条件下抛出错误.

                  Note: I removed the is_array because at that point $node has to be an array or throw an error at the first branch condition.

                  这篇关于PHP 不会中断递归 foreach 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 foreach 循环参数中分解数组 下一篇:在 simpleXMLElement 的 foreach 循环中获取子索引

                  相关文章

                  最新文章

                  • <bdo id='ia7is'></bdo><ul id='ia7is'></ul>

                  <legend id='ia7is'><style id='ia7is'><dir id='ia7is'><q id='ia7is'></q></dir></style></legend>

                    <small id='ia7is'></small><noframes id='ia7is'>

                      <tfoot id='ia7is'></tfoot>

                    1. <i id='ia7is'><tr id='ia7is'><dt id='ia7is'><q id='ia7is'><span id='ia7is'><b id='ia7is'><form id='ia7is'><ins id='ia7is'></ins><ul id='ia7is'></ul><sub id='ia7is'></sub></form><legend id='ia7is'></legend><bdo id='ia7is'><pre id='ia7is'><center id='ia7is'></center></pre></bdo></b><th id='ia7is'></th></span></q></dt></tr></i><div id='ia7is'><tfoot id='ia7is'></tfoot><dl id='ia7is'><fieldset id='ia7is'></fieldset></dl></div>