当前位置:网站首页>学习使用php实现无限极评论和无限极转二级评论解决方案

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

2022-06-09 20:22:00 游戏编程

评论数组

$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_listArray(    [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_listArray(    [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] => 回复                        )                )        ))

作者:徊忆羽菲

游戏编程,一个游戏开发收藏夹~

如果图片长时间未显示,请使用Chrome内核浏览器。

原网站

版权声明
本文为[游戏编程]所创,转载请带上原文链接,感谢
https://www.233tw.com/php/123661