使用PHP将注释排序到树结构中

时间:2014-02-27 15:13:59

标签: php arrays sorting multidimensional-array tree

我使用以下字段格式从我的mysql表中选择了我的评论,

comment_id,parent_id,group_id,message

我有一个sort函数,可以将这组数据分类为树,多维数组。

父母存储在树的第一级,其余的,即回复存储在其关联父母的子数组中。

我已经测试了我的功能,结果不是我想要的,我发送了7条评论,并且返回了11条,我发现给我这个错误的行是:

$all[$id] = $entry; //put entry in array

但是需要处理下一行,这是函数的关键

$all[$pid]['child'][$id] =& $all[$id]; //add entry to parents child array 

这是功能:

function sort_comments($input){

$output = array();
$all = array();
$dangling = array();

foreach ($input as $entry) {

    $id = $entry['comment_id'];
    $entry['child'] = array();

    if (empty($entry['parent_id'])){
        $all[$id] = $entry;
        $output[] =& $all[$id];
    } 
    else {
        $dangling[$id] = $entry; 
    }
}

//Process all 'dangling' nodesW
while (count($dangling) > 0) {
    foreach($dangling as $entry) {
        $id = $entry['comment_id'];
        $pid = $entry['parent_id'];
        // If the parent has already been added to the output, it's
        // safe to add this node too
        if (isset($all[$pid])) { //if parent is set in the array
            $all[$id] = $entry; //put entry in array
            $all[$pid]['child'][$id] =& $all[$id]; //add entry to parents child array 
            unset($dangling[$entry['comment_id']]); //unset the dangling entry
        }
    }
}

return $all;

}

输入数组是:

array (
  0 => 
  array (
    'comment_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'You can do that, easy peasy!',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2013-11-19 14:34:41',
  ),
  1 => 
  array (
    'comment_id' => '68911c41a8cb13742dfd16f299aa3a2c9e87e16d',
    'profile_id' => '1dd36ac747735a3ee8a1d47750e1515ab7ac0d53',
    'name' => 'James Boyd',
    'parent_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
    'comment' => 'hello chris',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2013-11-27 15:40:31',
  ),
  2 => 
  array (
    'comment_id' => 'b94ce651fc010917e6b18fa6677c0941bacdcab0',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'Testing',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-20 16:11:38',
  ),
  3 => 
  array (
    'comment_id' => '83b18d5f5f57ada93cf06651b037e6fd6bac0d5a',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'Test comment!',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-20 16:37:16',
  ),
  4 => 
  array (
    'comment_id' => 'a96ff2e144ea02e6a9666e394be491cfc0372e16',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'Test 14:16',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-21 14:16:29',
  ),
  5 => 
  array (
    'comment_id' => '7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
    'comment' => 'Test 14:17',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-21 14:17:10',
  ),
  6 => 
  array (
    'comment_id' => '3b221cd10ca36f7b723f19d6f53d87e42878280c',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '68911c41a8cb13742dfd16f299aa3a2c9e87e16d',
    'comment' => 'Testing 14:23',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-21 14:23:44',
  ),
)

实际输出:

array (
  'e465ce0a5301b8ed2eb66be06f768184f7727e3a' => 
  array (
    'comment_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'You can do that, easy peasy!',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2013-11-19 14:34:41',
    'child' => 
    array (
      '68911c41a8cb13742dfd16f299aa3a2c9e87e16d' => 
      array (
        'comment_id' => '68911c41a8cb13742dfd16f299aa3a2c9e87e16d',
        'profile_id' => '1dd36ac747735a3ee8a1d47750e1515ab7ac0d53',
        'name' => 'James Boyd',
        'parent_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
        'comment' => 'hello chris',
        'type' => 'a',
        'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
        'registered' => '2013-11-27 15:40:31',
        'child' => 
        array (
          '3b221cd10ca36f7b723f19d6f53d87e42878280c' => 
          array (
            'comment_id' => '3b221cd10ca36f7b723f19d6f53d87e42878280c',
            'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
            'name' => 'Chris Moore',
            'parent_id' => '68911c41a8cb13742dfd16f299aa3a2c9e87e16d',
            'comment' => 'Testing 14:23',
            'type' => 'a',
            'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
            'registered' => '2014-02-21 14:23:44',
            'child' => 
            array (
            ),
          ),
        ),
      ),
      '7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d' => 
      array (
        'comment_id' => '7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d',
        'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
        'name' => 'Chris Moore',
        'parent_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
        'comment' => 'Test 14:17',
        'type' => 'a',
        'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
        'registered' => '2014-02-21 14:17:10',
        'child' => 
        array (
        ),
      ),
    ),
  ),
  'b94ce651fc010917e6b18fa6677c0941bacdcab0' => 
  array (
    'comment_id' => 'b94ce651fc010917e6b18fa6677c0941bacdcab0',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'Testing',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-20 16:11:38',
    'child' => 
    array (
    ),
  ),
  '83b18d5f5f57ada93cf06651b037e6fd6bac0d5a' => 
  array (
    'comment_id' => '83b18d5f5f57ada93cf06651b037e6fd6bac0d5a',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'Test comment!',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-20 16:37:16',
    'child' => 
    array (
    ),
  ),
  'a96ff2e144ea02e6a9666e394be491cfc0372e16' => 
  array (
    'comment_id' => 'a96ff2e144ea02e6a9666e394be491cfc0372e16',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '',
    'comment' => 'Test 14:16',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-21 14:16:29',
    'child' => 
    array (
    ),
  ),
  '68911c41a8cb13742dfd16f299aa3a2c9e87e16d' => 
  array (
    'comment_id' => '68911c41a8cb13742dfd16f299aa3a2c9e87e16d',
    'profile_id' => '1dd36ac747735a3ee8a1d47750e1515ab7ac0d53',
    'name' => 'James Boyd',
    'parent_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
    'comment' => 'hello chris',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2013-11-27 15:40:31',
    'child' => 
    array (
      '3b221cd10ca36f7b723f19d6f53d87e42878280c' => 
      array (
        'comment_id' => '3b221cd10ca36f7b723f19d6f53d87e42878280c',
        'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
        'name' => 'Chris Moore',
        'parent_id' => '68911c41a8cb13742dfd16f299aa3a2c9e87e16d',
        'comment' => 'Testing 14:23',
        'type' => 'a',
        'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
        'registered' => '2014-02-21 14:23:44',
        'child' => 
        array (
        ),
      ),
    ),
  ),
  '7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d' => 
  array (
    'comment_id' => '7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => 'e465ce0a5301b8ed2eb66be06f768184f7727e3a',
    'comment' => 'Test 14:17',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-21 14:17:10',
    'child' => 
    array (
    ),
  ),
  '3b221cd10ca36f7b723f19d6f53d87e42878280c' => 
  array (
    'comment_id' => '3b221cd10ca36f7b723f19d6f53d87e42878280c',
    'profile_id' => '8fa7a1679560876eaf2f8060abd916b692c719dc',
    'name' => 'Chris Moore',
    'parent_id' => '68911c41a8cb13742dfd16f299aa3a2c9e87e16d',
    'comment' => 'Testing 14:23',
    'type' => 'a',
    'ambition_id' => '85c39f39553d4a004778b8936fb5084daa77c80d',
    'registered' => '2014-02-21 14:23:44',
    'child' => 
    array (
    ),
  ),
)

预期的输出:

Array
(
    [e465ce0a5301b8ed2eb66be06f768184f7727e3a] => Array
        (
            [comment_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
            [name] => Chris Moore
            [parent_id] => 
            [comment] => You can do that, easy peasy!
            [type] => a
            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
            [registered] => 2013-11-19 14:34:41
            [child] => Array
                (
                    [68911c41a8cb13742dfd16f299aa3a2c9e87e16d] => Array
                        (
                            [comment_id] => 68911c41a8cb13742dfd16f299aa3a2c9e87e16d
                            [profile_id] => 1dd36ac747735a3ee8a1d47750e1515ab7ac0d53
                            [name] => James Boyd
                            [parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
                            [comment] => hello chris
                            [type] => a
                            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
                            [registered] => 2013-11-27 15:40:31
                            [child] => Array
                                (
                                    [3b221cd10ca36f7b723f19d6f53d87e42878280c] => Array
                                        (
                                            [comment_id] => 3b221cd10ca36f7b723f19d6f53d87e42878280c
                                            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
                                            [name] => Chris Moore
                                            [parent_id] => 68911c41a8cb13742dfd16f299aa3a2c9e87e16d
                                            [comment] => Testing 14:23
                                            [type] => a
                                            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
                                            [registered] => 2014-02-21 14:23:44
                                            [child] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d] => Array
                        (
                            [comment_id] => 7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d
                            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
                            [name] => Chris Moore
                            [parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
                            [comment] => Test 14:17
                            [type] => a
                            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
                            [registered] => 2014-02-21 14:17:10
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [b94ce651fc010917e6b18fa6677c0941bacdcab0] => Array
        (
            [comment_id] => b94ce651fc010917e6b18fa6677c0941bacdcab0
            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
            [name] => Chris Moore
            [parent_id] => 
            [comment] => Testing
            [type] => a
            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
            [registered] => 2014-02-20 16:11:38
            [child] => Array
                (
                )

        )

    [83b18d5f5f57ada93cf06651b037e6fd6bac0d5a] => Array
        (
            [comment_id] => 83b18d5f5f57ada93cf06651b037e6fd6bac0d5a
            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
            [name] => Chris Moore
            [parent_id] => 
            [comment] => Test comment!
            [type] => a
            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
            [registered] => 2014-02-20 16:37:16
            [child] => Array
                (
                )

        )

    [a96ff2e144ea02e6a9666e394be491cfc0372e16] => Array
        (
            [comment_id] => a96ff2e144ea02e6a9666e394be491cfc0372e16
            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
            [name] => Chris Moore
            [parent_id] => 
            [comment] => Test 14:16
            [type] => a
            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
            [registered] => 2014-02-21 14:16:29
            [child] => Array
                (
                )

        )
)

它再次添加了几个条目,因此我在$ all数组中输入了11个条目而不是预期的7条。

你能看出出现了什么问题以及如何修复我的排序评论功能吗?

谢谢

1 个答案:

答案 0 :(得分:2)

看起来是时候打破好的'递归函数了:

function sort_r($input, $parentId) {
    $output = array();
    foreach ($input as $key => $item) {
        // Look for a child of parent
        if ($item['parent_id'] == $parentId) {
            // Find its children recursively
            $item['children'] = sort_r($input, $item['comment_id']);
            $output[] = $item;
        }
    }
    return $output;
}

致电:

$sortedArray = sort_r($input, '');

就是这样。