我开始从id = 1的项目开始挖掘它的chils和顶层 foreach从来没有到达元素3 !!!顶级foreach停止。 <?php 函数prn($ s){ echo($ s) 。''< br>''); } $ nodes = array(); $ nodes [ 0] =新节点(1,0); $ nodes [1] =新节点(2,1); $ nodes [2] =新节点( 3,1); $ nodes [3] =新节点(4,0); 类节点{ var $ id; var $ parentId; 函数节点($ id,$ parentId){ $ this-> ; id = $ id; $ this-> parentId = $ parentId; } 函数挖掘($ level) ){$ / 全球$节点; $ l =''''; for($ i = 0; $ i< $ level; $ i ++)$ l。=''..''; prn($ l。''寻找孩子''。$ this-> id。'' {''); foreach($节点为$ n){ prn($ l。$ n-> id。'''(父母''。$ n-> parentId。'')''); if($ n-> parentId == $ this-> id){ $ n-> dig($ level + 1); } } prn($ l。''}''); } } $ nodes [0] - > dig(0); ? > 输出应如下所示: 寻找1的孩子{ 1(父母0) 2(父母1) ...寻找2的孩子{ ... 1(父母0) ... 2(父母1) ... 3(父母1) ...} 3(父母1) ...寻找3个孩子{ ... 1(父母0) ... 2 (父母1) ... 3(父母1) ...} } 但它看起来像这样: 寻找1的孩子{ 1(父母0) 2(父母1) ...寻找2的孩子{ ... 1(父母0) ... 2(父母1) ... 3(父母1) ...} } Hi! Looks like there is a bug in php. If I have function which usesforeach to run trough array recursively, the lop-level foreachinterupted by lover-level foreach''es. If I use simply ''for'' everythingis ok. Example: I have an array of 3 objects connected hierarchically by theirvariables id and parentId, here is the hierarchy:id=1 parentId=0....id=2 parentId=1....id=3 parentId=1 I start digging from item with id=1 down to its chils and top-levelforeach NEVER REACH ELEMENT 3!!! Top-level foreach just stops. <?phpfunction prn($s){echo($s.''<br>'');} $nodes = array();$nodes[0] = new node(1, 0);$nodes[1] = new node(2, 1);$nodes[2] = new node(3, 1);$nodes[3] = new node(4, 0); classnode{var $id;var $parentId; function node($id, $parentId){$this->id = $id;$this->parentId = $parentId;} function dig($level){global $nodes; $l = '''';for($i=0; $i<$level; $i++) $l.=''..'';prn($l.''looking for childs of ''.$this->id.'' {''); foreach($nodes as $n){prn($l.$n->id.'' (parent ''.$n->parentId.'')'');if($n->parentId == $this->id){$n->dig($level+1);}} prn($l.''}'');} } $nodes[0]->dig(0); ?> The output should look like this: looking for childs of 1 {1 (parent 0)2 (parent 1)...looking for childs of 2 {...1 (parent 0)...2 (parent 1)...3 (parent 1)...}3 (parent 1)...looking for childs of 3 {...1 (parent 0)...2 (parent 1)...3 (parent 1)...}} But it looks like this: looking for childs of 1 {1 (parent 0)2 (parent 1)...looking for childs of 2 {...1 (parent 0)...2 (parent 1)...3 (parent 1)...}} 推荐答案 s){ echo(s){echo( s。''< br>''); } s.''<br>'');} nodes = array( ); nodes = array(); 这篇关于对于大师:php中的bug?递归的foreach不能正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 10:44