Zend框架foreach循环停止吃第一次迭代(Zend framework foreach loop stop ate the first iteration)

编程入门 行业动态 更新时间:2024-10-10 02:22:39
Zend框架foreach循环停止吃第一次迭代(Zend framework foreach loop stop ate the first iteration)

我实际上在研究ZF。 我有一个类别表,我想创建一个树,以便显示如下数据:

Category --Sub cat 1 --Sub cat 2 ----Su sub cat 1 Another Category --Sub cat 1 //...etc...

我正在使用fetchAll方法获取所有数据。 一切正常。 但后来我正在尝试将我的树创建为双foreach循环,如下所示:

$tree = array(); foreach($data as $parent){ $tree[$parent->name] = array(); foreach($data as $child){ if($child->parent_id == $parent->id){ $tree[$parent->name][] = $child->name; } } }

问题是循环在main循环第一次迭代之后停止,所以我只是得到第一个父类和它的子类,但它不会继续到第二个父类。

我的数据库表如下:

id, name, parent_id

任何想法?

编辑感谢Thibault,它确实使用了旧的for循环:

for($i=0;$i<count($data);$i++){ $tree[$data[$i]->name] = array(); for($j=0;$j<count($data);$j++){ if($data[$j]->parent_id == $data[$i]->id){ $tree[$data[$i]->name][] = $data[$j]->name; } } }

I'm actually working on ZF. I have a category table with which, I want to create a tree in order to get display the data as below :

Category --Sub cat 1 --Sub cat 2 ----Su sub cat 1 Another Category --Sub cat 1 //...etc...

I'm using the fetchAll method to get all my data. Everyting works fine. But then I'm now trying to create my tree into a double foreach loop as below :

$tree = array(); foreach($data as $parent){ $tree[$parent->name] = array(); foreach($data as $child){ if($child->parent_id == $parent->id){ $tree[$parent->name][] = $child->name; } } }

The problem is that the loop stop after the main loop first iteration so I'm just getting the first parent and it's sub category but it does not continue to the second parent.

My database table as the following fields :

id, name, parent_id

Any idea?

EDIT Thanks to you Thibault, it did work using the good old for loop :

for($i=0;$i<count($data);$i++){ $tree[$data[$i]->name] = array(); for($j=0;$j<count($data);$j++){ if($data[$j]->parent_id == $data[$i]->id){ $tree[$data[$i]->name][] = $data[$j]->name; } } }

最满意答案

两个$data变量的游标之间可能存在冲突。

您应该为第二个foreach循环使用$data的副本。

或者使用带有$i和$j索引的循环,并通过$data[$i]和$data[$j]调用它们来访问数组,因此循环不会搞砸。

编辑快乐我可以帮助,但经过一些研究,我创建了这段代码:

<? class o { public $id; public $name; public $parent_id; function __construct($_id,$_name,$_parent) { $this->id = $_id; $this->name = $_name; $this->parent_id = $_parent; } } $data = array( new o(1,'toto',0), new o(2,'truc',1), new o(3,'machin',1), new o(4,'bidule',2), new o(5,'titi',3), new o(6,'tutu',3), ); $tree = array(); foreach($data as $parent){ $tree[$parent->name] = array(); foreach($data as $child){ if($child->parent_id == $parent->id){ $tree[$parent->name][] = $child->name; } } } print_r($tree);

你的代码工作得很好:(其他地方的东西一定是错的...)

Array ( [toto] => Array ( [0] => truc [1] => machin ) [truc] => Array ( [0] => bidule ) [machin] => Array ( [0] => titi [1] => tutu ) [bidule] => Array ( ) [titi] => Array ( ) [tutu] => Array ( ) )

You may have a conflict between the cursor of both $data variables.

You should use a copy of $data for the second foreach loop.

Or use for loops with $i and $j index, and call them via $data[$i] and $data[$j] to access the array, so the loops don't get messed up.

EDIT Happy i could help, but after some research, i created this piece of code :

<? class o { public $id; public $name; public $parent_id; function __construct($_id,$_name,$_parent) { $this->id = $_id; $this->name = $_name; $this->parent_id = $_parent; } } $data = array( new o(1,'toto',0), new o(2,'truc',1), new o(3,'machin',1), new o(4,'bidule',2), new o(5,'titi',3), new o(6,'tutu',3), ); $tree = array(); foreach($data as $parent){ $tree[$parent->name] = array(); foreach($data as $child){ if($child->parent_id == $parent->id){ $tree[$parent->name][] = $child->name; } } } print_r($tree);

And your code works just fine : (something must be wrong somewhere else ...)

Array ( [toto] => Array ( [0] => truc [1] => machin ) [truc] => Array ( [0] => bidule ) [machin] => Array ( [0] => titi [1] => tutu ) [bidule] => Array ( ) [titi] => Array ( ) [tutu] => Array ( ) )

更多推荐

本文发布于:2023-08-07 11:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   迭代   foreach   Zend   framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!