【问题标题】:Sorting comments into a multidimensional array using recursion PHP使用递归 PHP 将评论排序到多维数组中
【发布时间】:2014-03-30 01:44:12
【问题描述】:

我使用 MYSQL 获取我的评论,格式为

comment_id, parent_id, group_id, message

如果评论是一级评论,则 parent_id 可以为空。

我存储我的嵌套 cmets,即在子数组中的回复,添加到我创建的函数中。

这是一个输出,包括1级和2级cmets,基本评论/回复:

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
            [child] => Array
                (
                    [0] => 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
                        )

                    [1] => 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
                        )
                )
        )
)

我使用以下功能来尝试对我的 cmets 进行排序,我设法让 1 级和 2 级 cmets 工作,但任何超过此的功能都不起作用。

这是我目前的功能:

getCommentsForParent - 递归调用,从第 2 级回复到第 n 级回复 和 sort_cmets - 设置初始数组和第一级 cmets

function getCommentsForParent($p, $nested, $type){

    $index = 0;

    if(!empty($nested)){
        foreach($nested as $n){
            if($p['comment_id'] == $n['parent_id']){
                $p['child'][] = $n;
                $i = array_search($n, $nested);     
                unset($nested[$i]);
                $array = getCommentsForParent($n, $nested, 'inner');
                $n = $array[0];
                $nested = $array[1];
            }

            $index++;
        }
    }

    return array($p, $nested);
}

function sort_comments($ar){

    //split the comments into 1st level and nth level
    $parents = array();
    $nested = array();

    foreach ($ar as $item) {
        if(empty($item['parent_id'])){
            $parents[] = $item;
        }
        else{
            $nested[] = $item;
        }
    }

    if(is_array($parents)){
        $index = 0;
        foreach ($parents as $p) {
            if(!empty($nested)){
                $array = getCommentsForParent($p, $nested, 'parent');
                $p = $array[0];
                $nested = $array[1];
                $parents[$index] = $p;
            }
            $index++;
        }
    }

    return $parents;
}

您能否尝试找到我的代码的解决方案,我确定我很接近。

【问题讨论】:

  • 你可以按照你想要的方式从数据库中查询它
  • 你能按照我解释过的格式给我看看吗?
  • @syb0rg,好的,我删除了评论。对不起,克里斯,我错了。

标签: php arrays sorting recursion multidimensional-array


【解决方案1】:

首先这个数据结构是一棵树。

您应该使用以下类递归地迭代它:ArrayIteratorRecursiveTreeIteratorRecursiveIteratorIterator

您可以使用任何array sort functions 对其进行排序。例如,使用usort,您可以使用回调作为比较器。如果顺序取决于子节点,您可以使用回调使排序递归而不是迭代器类。

祝你好运!

【讨论】:

    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2021-08-23
    • 2019-02-20
    • 2020-03-21
    • 2014-10-31
    相关资源
    最近更新 更多