pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

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

    1. <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 class="vupqp8b" id='9Rhhu'><tfoot id='9Rhhu'></tfoot><dl id='9Rhhu'><fieldset id='9Rhhu'></fieldset></dl></div>
      • <bdo id='9Rhhu'></bdo><ul id='9Rhhu'></ul>

        PHP 不會中斷遞歸 foreach 循環

        Php doesn#39;t break in a recursive foreach loop(PHP 不會中斷遞歸 foreach 循環)
        <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 class="3d3tozn" id='uypGf'><tfoot id='uypGf'></tfoot><dl id='uypGf'><fieldset id='uypGf'></fieldset></dl></div>
      1. <tfoot id='uypGf'></tfoot>

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

              <tbody id='uypGf'></tbody>

                • <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 循環的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個像下面這樣的遞歸函數.

                  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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)
                  • <bdo id='ia7is'></bdo><ul id='ia7is'></ul>
                      <tbody id='ia7is'></tbody>

                    <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>

                          • <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 class="3mis8pc" id='ia7is'><tfoot id='ia7is'></tfoot><dl id='ia7is'><fieldset id='ia7is'></fieldset></dl></div>
                            主站蜘蛛池模板: 5nd音乐网|最新流行歌曲|MP3歌曲免费下载|好听的歌|音乐下载 免费听mp3音乐 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 直读光谱仪,光谱分析仪,手持式光谱仪,碳硫分析仪,创想仪器官网 | 能耗监测系统-节能监测系统-能源管理系统-三水智能化 | 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 灌木树苗-绿化苗木-常绿乔木-价格/批发/基地 - 四川成都途美园林 | 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 单电机制砂机,BHS制砂机,制沙机设备,制砂机价格-正升制砂机厂家 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 齿辊分级破碎机,高低压压球机,立式双动力磨粉机-郑州长城冶金设备有限公司 | 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 水厂自动化|污水处理中控系统|水利信息化|智慧水务|智慧农业-山东德艾自动化科技有限公司 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 陕西视频监控,智能安防监控,安防系统-西安鑫安5A安防工程公司 | 超声波清洗机-超声波清洗设备定制生产厂家 - 深圳市冠博科技实业有限公司 | 新疆乌鲁木齐网站建设-乌鲁木齐网站制作设计-新疆远璨网络 | POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 动环监控_机房环境监控_DCIM_机房漏水检测-斯特纽 | 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 中矗模型-深圳中矗模型设计有限公司| 美名宝起名网-在线宝宝、公司、起名平台 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 深圳律师咨询_深圳律师事务所_华荣【免费在线法律咨询】网 | 运动木地板价格,篮球馆体育运动木地板生产厂家_欧氏地板 | 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 | 流水线电子称-钰恒-上下限报警电子秤-上海宿衡实业有限公司 | 定制/定做冲锋衣厂家/公司-订做/订制冲锋衣价格/费用-北京圣达信 | 广西资质代办_建筑资质代办_南宁资质代办理_新办、增项、升级-正明集团 | 气动隔膜阀_气动隔膜阀厂家_卫生级隔膜阀价格_浙江浙控阀门有限公司 | 钢木实验台-全钢实验台-化验室通风柜-实验室装修厂家-杭州博扬实验设备 | 深圳律师咨询_深圳律师事务所_华荣【免费在线法律咨询】网 | 江西自考网-江西自学考试网| 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 防水套管_柔性防水套管_刚性防水套管-巩义市润达管道设备制造有限公司 | 直流大电流电源,燃料电池检漏设备-上海政飞 | 元拓建材集团官方网站| 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 湖北省煤炭供应链综合服务平台| 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 |