简体   繁体   中英

Sorting comments into a multidimensional array using recursion PHP

I fetch my commments using MYSQL, with the format

comment_id, parent_id, group_id, message

Where the parent_id can be empty if the comment is a 1st level comment.

I store my nested comments, ie replies in a child array, added within function that I have made.

Here is an output, including 1st level and 2nd level comments, basic comment/reply:

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

I have the following functions that I use to try and sort my comments, I manage to get 1st level and 2nd level comments working, but anything over that doesnt work.

Here are my current functions:

getCommentsForParent - a recursive call, to go from 2nd level to nth level replies and sort_comments - to setup the inital arrays and 1st level comments

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

Could you try and find a solution to my code, i'm sure that I am close.

First of all this data structure is a tree.

You should iterate it recursively using the following classes: ArrayIterator , RecursiveTreeIterator and RecursiveIteratorIterator .

You can sort it using any of the array sort functions . For example with usort you can use a callback as a comparator. If the order depends on the child nodes, you can use the callback to make the sort recursive instead of the iterator classes.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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