繁体   English   中英

使用递归PHP将注释排序到多维数组中

[英]Sorting comments into a multidimensional array using recursion PHP

我使用格式为MYSQL的评论

comment_id, parent_id, group_id, message

如果注释是第一级注释,则parent_id可以为空。

我将嵌套的注释(即答复存储在子数组中)存储在我所做的函数中。

这是输出,包括第一级和第二级注释,基本注释/答复:

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
                        )
                )
        )
)

我具有以下用于尝试对评论进行排序的功能,我设法使第一级和第二级评论正常工作,但是超出此范围的所有操作均无效。

这是我当前的功能:

getCommentsForParent-递归调用,从第2级到第n级答复和sort_comments-设置初始数组和第1级注释

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;
}

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

首先,此数据结构是一棵树。

您应该使用以下类递归地对其进行迭代: ArrayIteratorRecursiveTreeIteratorRecursiveIteratorIterator

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

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM