2013-09-29 207 views
0

我有一个帖子的碰撞,我想在表中显示。 每篇文章都有一个id和一个parent_id。php foreach在foreach

我想通过嵌套的foreach循环表,但我没有得到它的工作。 我的想法是遍历每个帖子,打印名称和东西,并找到它的所有孩子(通过循环浏览每个帖子,看看孩子的父母ID是否与我的ID相同。)

我的代码是:

$allPosts = $posts; 
foreach ($posts as $post) { 
    if ($post->parent_id == 0){ //0 means highest level, no parent 
     echo $post->name; 
     $current_id = $post->id; 

     foreach ($allPosts as $childPost){ 
      if($childPost->parent_id == $current_id){ 
       echo $cihldPost->name; 

      } 
     } 
    } 
} 

问题是,第一个循环只能运行一次。如果我删除第二个foreach循环,第一个正确运行。

为什么会发生这种情况,我该如何解决?

添加: 该帖子本身就像一篇博文,它包含以下内容:id,title,body,compact,parent_id。其中,id是唯一的ID,标题和正文作为博客文章的标题和正文,compact是用于url的简短名称,是用于告知帖子正躺在谁的父ID,即。我是我父母的孩子。

由于用户应该有所有可能性来移动帖子并将帖子放在菜单中,所以将顶级帖子视为菜单项,并且将父级视为子菜单项。

+1

'$ childPost','$ allPosts'包含什么?关系是什么? –

+0

$ allPosts = $帖子,只是为了使变量不同,即使它是相同的帖子。 的childpost(其中doesn't必须是childpost除非其PARENT_ID == current_id),是后我得到的每一个的只是名字。 每个帖子包含姓名,身份证,PARENT_ID,内容,URL以及其他次要的东西。把它看作是一篇博客文章。这有道理吗? – Malin

+0

@Malin如果您发布的'$帖子一些样本内容;',它会变得容易让我们理解您的问题 –

回答

0

出于某种原因,该做的foreach不适用于我的Controller传递给我的视图的$ posts对象本身。但是,如果我将$ posts保存在本地数组中,则一切正常。我仍然不知道为什么我有这个问题,所以请评论或提供一个更好的答案,这一点。

$mylist = array(); 
foreach($posts as $post) { 
    $listitem = array(); 
    $listitem['id'] = $post->id; 
    $listitem['parent_id'] = $post->parent_id; 
    $listitem['title'] = $post->title; 
    $listitem['compact'] = $post->compact; 
    $mylist[] = $listitem; 
} 
foreach($mylist as $listitem){ 
    #code.. 
    foreach($mylist as $inneritem){ 
     #code.. 
    } 
} 
+0

我想你的代码的财产后,我纠正错字'$ cihldPost->名;'它的所有工作,如它应该。除此之外,我只在输出中添加换行符。如果您需要,我可以发布我尝试过的代码。 – sudee

+0

是的请做,我也纠正了类型,并没有得到它的工作,除了我上面提供的解决方案 – Malin

2

My idea is to loop through each post, print the name and stuff, and also find all it´s children (by looping through each post and see if the childs parent id is equal to my id.)

目前,您只是试图再次循环同一个对象。 $allPosts变量不是必需的。如果您尝试循环访问每个子元素,则需要在嵌套的foreach循环内使用$post

你目前做:

$obj2 = $obj; 
foreach ($obj as $child) { 
    foreach ($obj2 as $anotherchild) { 
    # code... 
    } 
} 

粗略地说,结构应该是这样的:

foreach ($obj as $child) { 
    foreach ($child as $secondchild) { 
     # code... 
    } 
} 

与您的代码:

foreach ($posts as $post) { 
    if ($post->parent_id == 0){ //0 means highest level, no parent 
     echo $post->name; 
     $current_id = $post->id; 

     foreach ($post as $childPost){ 
      if($childPost->parent_id == $current_id){ 
       echo $cihldPost->name; // <-- typo?  
      } 
     } 
    } 
} 
+0

补充说,不为我工作,因为$帖子是一个包含所有帖子的载体,以及$ post是一个仅包含自己的实际帖子 – Malin

+0

@Malin:你是不是想循环遍历所有帖子?那么你到底想用嵌套的foreach做什么呢? –

+0

是,通过所有的后 第一循环:帖子打印的名字。然后打印的名字,我孩子的 – Malin

0

这是我如何改变你的代码:

$posts = array(
    (object) array("parent_id" => 0, "id" => 1, "name" => 'Outer 1'), 
    (object) array("parent_id" => 0, "id" => 2, "name" => 'Outer 2'), 
    (object) array("parent_id" => 0, "id" => 3, "name" => 'Outer 3'), 
    (object) array("parent_id" => 0, "id" => 4, "name" => 'Outer 4'), 
    (object) array("parent_id" => 0, "id" => 5, "name" => 'Outer 5'), 
    (object) array("parent_id" => 1, "id" => 6, "name" => 'Inner 1.1'), 
    (object) array("parent_id" => 1, "id" => 7, "name" => 'Inner 1.2'), 
    (object) array("parent_id" => 4, "id" => 8, "name" => 'Inner 4.1'), 
    (object) array("parent_id" => 5, "id" => 9, "name" => 'Inner 5.2'), 
    (object) array("parent_id" => 5, "id" => 10, "name" => 'Inner 5.2'), 
    (object) array("parent_id" => 5, "id" => 11, "name" => 'Inner 5.3'), 

); 

$allPosts = $posts; 
foreach ($posts as $post) { 
    if ($post->parent_id == 0){ //0 means highest level, no parent 
     echo $post->name . ' : '; 
     $current_id = $post->id; 

     foreach ($allPosts as $childPost){ 
      if($childPost->parent_id == $current_id){ 
       echo $childPost->name . ', '; 
      } 
     } 
     echo '<br>'; 
    } 
} 

输出如下:

Outer 1 : Inner 1.1, Inner 1.2, 
Outer 2 : 
Outer 3 : 
Outer 4 : Inner 4.1, 
Outer 5 : Inner 5.2, Inner 5.2, Inner 5.3, 

如果问题是不是在你的代码(这看起来不错给我,还按照我的计算机上的预期工作)它可能在$posts阵列的结构中。尝试使用变量var_dump以查看它们是否包含不完整或不正确的数据。