对于大师:php中的bug?递归的foreach不能正常工作。 [英] For gurus: bug in php? Recursive foreach dont work properly.

查看:67
本文介绍了对于大师:php中的bug?递归的foreach不能正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!看起来php中有一个bug。如果我有一个使用

foreach以递归方式运行数组的函数,那么lop级别的foreach

会被情人级别的foreach''es中断。如果我只是简单地使用''for''一切

就可以了。示例:


我有一个由3个对象组成的数组,它们通过

变量id和parentId按层次结构连接,这里是层次结构:

id = 1 parentId = 0

.... id = 2 parentId = 1

.... id = 3 parentId = 1

我开始从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。''}'');

} < br $>

}


$ 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 uses
foreach to run trough array recursively, the lop-level foreach
interupted by lover-level foreach''es. If I use simply ''for'' everything
is ok. Example:

I have an array of 3 objects connected hierarchically by their
variables 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-level
foreach NEVER REACH ELEMENT 3!!! Top-level foreach just stops.

<?php
function 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);

class node{
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不能正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆