学习使用php实现无限极评论和无限极转二级评论解决方案

学习使用php实现无限极评论和无限极转二级评论解决方案

评论数组

$parent_comment_id = 317;

$comment_str = '[{"comment_id":326,"parent_comment_id":317,"reply_comment_id":319,"like_number":0,"is_like":0,"comment_time":"2022-06-02 22:25:41","user_name":"奇葩250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":"奇葩天地网总部•奇葩250部","comment_content":"你字多 你说的对"},{"comment_id":325,"parent_comment_id":317,"reply_comment_id":317,"like_number":0,"is_like":0,"comment_time":"2022-06-02 22:01:28","user_name":"奇葩250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":"奇葩天地网总部•奇葩250部","comment_content":"好 继续努力"},{"comment_id":324,"parent_comment_id":317,"reply_comment_id":319,"like_number":0,"is_like":0,"comment_time":"2022-06-02 21:46:43","user_name":"奇葩250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":"奇葩天地网总部•奇葩250部","comment_content":"我觉得你说的对"},{"comment_id":319,"parent_comment_id":317,"reply_comment_id":318,"like_number":0,"is_like":0,"comment_time":"2022-05-26 22:47:55","user_name":"奇葩250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":"奇葩天地网总部•奇葩250部","comment_content":"三级评论🤔"},{"comment_id":318,"parent_comment_id":317,"reply_comment_id":317,"like_number":0,"is_like":0,"comment_time":"2022-05-26 22:45:20","user_name":"奇葩250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":"奇葩天地网总部•奇葩250部","comment_content":"评论中的评论"}]';


$wenku_comment_tmp = json_decode($comment_str, true);

echo "<pre>";
print_r($wenku_comment_tmp);

$wenku_comment_ay_tmp = [];
//不用每次都查询数据库
foreach ($wenku_comment_tmp as $k => $v) {
    $wenku_comment_ay_tmp[$v['comment_id']] = $v;
}

无限极评论

//评论详情
function getWenkuCommentDetailAll($wenku_comment_ay_tmp, $data, $parent_comment_id)
{

    $wenku_comment_list = [];
    foreach ($data as $k => $v) {


        //如果上一级的评论id和父级id一致 则放入子级中
        if ($parent_comment_id == $v['reply_comment_id']) {

            $reply_comment_user = $reply_tip = '';
            //如果父级id和回复id,不是同一个则需要增加 回复 字样
            if ($v['parent_comment_id'] != $v['reply_comment_id']) {
                $reply_tip = '回复';
                $reply_comment_user = $wenku_comment_ay_tmp[$v['reply_comment_id']]['user_name'];
            }

            $child_list = getWenkuCommentDetailAll($wenku_comment_ay_tmp, $data, $v['comment_id']);

            //对子级评论,做排序
            if ($child_list) {
                $comment_id = array_column($child_list, 'comment_id');
                array_multisort($comment_id, SORT_ASC, $child_list);
            }

            $wenku_comment_list[] = array(
                'comment_id' => $v['comment_id'],
                'parent_comment_id' => (int)$v['parent_comment_id'],
                'reply_comment_id' => (int)$v['reply_comment_id'],
                'reply_comment_user' => $reply_comment_user,
                'reply_tip' => $reply_tip,
                'like_number' => (int)$v['like_number'],//点赞数
                'is_like' => (int)$v['is_like'],//是否点过赞
                'comment_time' => $v['comment_time'],//评论日期
                'user_name' => $v['user_name'],
                'department_name' => $v['department_name'],//评论人所在部门
                'comment_content' => $v['comment_content'],//评论内容
                'comment_user_id' => (int)$v['comment_user_id'],//评论用户id
                'comment_user_header' => $v['comment_user_header'],//评论人头像
                'list' => $child_list ?: [],
            );
            unset($reply_tip, $reply_comment_user, $child_list);
        }

    }
    return $wenku_comment_list;
}

$company_user_comment_list = getWenkuCommentDetailAll($wenku_comment_ay_tmp, $wenku_comment_tmp, $parent_comment_id);
echo "无限极company_user_comment_list<pre>";
print_r($company_user_comment_list);

打印结果

无限极company_user_comment_list

Array
(
    [0] => Array
        (
            [comment_id] => 325
            [parent_comment_id] => 317
            [reply_comment_id] => 317
            [reply_comment_user] => 
            [reply_tip] => 
            [like_number] => 0
            [is_like] => 0
            [comment_time] => 2022-06-02 22:01:28
            [user_name] => 奇葩250
            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 好 继续努力
            [comment_user_id] => 14583
            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
            [list] => Array
                (
                )

        )

    [1] => Array
        (
            [comment_id] => 318
            [parent_comment_id] => 317
            [reply_comment_id] => 317
            [reply_comment_user] => 
            [reply_tip] => 
            [like_number] => 0
            [is_like] => 0
            [comment_time] => 2022-05-26 22:45:20
            [user_name] => 奇葩250
            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 评论中的评论
            [comment_user_id] => 14583
            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
            [list] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 319
                            [parent_comment_id] => 317
                            [reply_comment_id] => 318
                            [reply_comment_user] => 奇葩250
                            [reply_tip] => 回复
                            [like_number] => 0
                            [is_like] => 0
                            [comment_time] => 2022-05-26 22:47:55
                            [user_name] => 奇葩250
                            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 三级评论🤔
                            [comment_user_id] => 14583
                            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
                            [list] => Array
                                (
                                    [0] => Array
                                        (
                                            [comment_id] => 324
                                            [parent_comment_id] => 317
                                            [reply_comment_id] => 319
                                            [reply_comment_user] => 奇葩250
                                            [reply_tip] => 回复
                                            [like_number] => 0
                                            [is_like] => 0
                                            [comment_time] => 2022-06-02 21:46:43
                                            [user_name] => 奇葩250
                                            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 我觉得你说的对
                                            [comment_user_id] => 14583
                                            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
                                            [list] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [comment_id] => 326
                                            [parent_comment_id] => 317
                                            [reply_comment_id] => 319
                                            [reply_comment_user] => 奇葩250
                                            [reply_tip] => 回复
                                            [like_number] => 0
                                            [is_like] => 0
                                            [comment_time] => 2022-06-02 22:25:41
                                            [user_name] => 奇葩250
                                            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 你字多 你说的对
                                            [comment_user_id] => 14583
                                            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
                                            [list] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)


二级评论

将三级以上的所有评论改为二级评论


function getlist($wenku_comment_ay_tmp, $list)
{
    $tmp = [];
    foreach ($list as $k => $v) {
        if ($v['reply_comment_id'] == 317) {
           
            $child_coment = getsubtree($wenku_comment_ay_tmp, $list, $v['comment_id']);
            $v['child'] = $child_coment;
            $tmp[] = $v;
            unset($child_coment);
        }
    }
    return $tmp;
}

function getsubtree($wenku_comment_ay_tmp, $list, $pid)
{
    $tmp = [];

    foreach ($list as $lk => $lv) {

        if ($lv['reply_comment_id'] == $pid) {
            unset($list[$lk]);
            $reply_comment_user = $reply_tip = '';
            //如果父级id和回复id,不是同一个则需要增加 回复 字样
            if ($lv['parent_comment_id'] != $lv['reply_comment_id']) {
                $reply_tip = '回复';
                $reply_comment_user = $wenku_comment_ay_tmp[$lv['reply_comment_id']]['user_name'];
            }

            //增加字段
            $lv['reply_comment_user'] = $reply_comment_user;
            $lv['reply_tip'] = $reply_tip;
            $tmp[] = $lv;
            $tmplist = getsubtree($wenku_comment_ay_tmp, $list, $lv['comment_id']);
            $tmp = array_merge($tmp, $tmplist);
        }
    }

    return $tmp;
}


$company_user_comment_list = getlist($wenku_comment_ay_tmp, $wenku_comment_tmp);

echo "两级company_user_comment_list<pre>";
print_r($company_user_comment_list);

打印结果

两级company_user_comment_list

Array
(
    [0] => Array
        (
            [comment_id] => 325
            [parent_comment_id] => 317
            [reply_comment_id] => 317
            [like_number] => 0
            [is_like] => 0
            [comment_time] => 2022-06-02 22:01:28
            [user_name] => 奇葩250
            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
            [comment_user_id] => 14583
            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 好 继续努力
            [child] => Array
                (
                )

        )

    [1] => Array
        (
            [comment_id] => 318
            [parent_comment_id] => 317
            [reply_comment_id] => 317
            [like_number] => 0
            [is_like] => 0
            [comment_time] => 2022-05-26 22:45:20
            [user_name] => 奇葩250
            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
            [comment_user_id] => 14583
            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 评论中的评论
            [child] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 319
                            [parent_comment_id] => 317
                            [reply_comment_id] => 318
                            [like_number] => 0
                            [is_like] => 0
                            [comment_time] => 2022-05-26 22:47:55
                            [user_name] => 奇葩250
                            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
                            [comment_user_id] => 14583
                            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 三级评论🤔
                            [reply_comment_user] => 奇葩250
                            [reply_tip] => 回复
                        )

                    [1] => Array
                        (
                            [comment_id] => 326
                            [parent_comment_id] => 317
                            [reply_comment_id] => 319
                            [like_number] => 0
                            [is_like] => 0
                            [comment_time] => 2022-06-02 22:25:41
                            [user_name] => 奇葩250
                            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
                            [comment_user_id] => 14583
                            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 你字多 你说的对
                            [reply_comment_user] => 奇葩250
                            [reply_tip] => 回复
                        )

                    [2] => Array
                        (
                            [comment_id] => 324
                            [parent_comment_id] => 317
                            [reply_comment_id] => 319
                            [like_number] => 0
                            [is_like] => 0
                            [comment_time] => 2022-06-02 21:46:43
                            [user_name] => 奇葩250
                            [comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
                            [comment_user_id] => 14583
                            [department_name] => 奇葩天地网总部•奇葩250[comment_content] => 我觉得你说的对
                            [reply_comment_user] => 奇葩250
                            [reply_tip] => 回复
                        )

                )

        )

)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值