当前位置:网站首页>学习使用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内核浏览器。
边栏推荐
- 看看人家那文本识别系统,那叫一个优雅!
- STM32 memory knowledge
- Qualcomm: it will adhere to the diversified OEM strategy and pay attention to the business cooperation conditions of Intel OEM
- 95后大厂程序员删库被判刑9个月
- 不止于观测|阿里云可观测技术峰会正式上线
- The best time to plant a tree is ten years ago, and the second is now (C language selection and cycle training topic)
- ASP Err. Detailed description of number error
- Jerry's map modification app_ config. Video of H_ Rec buffer mode and output frame rate [chapter]
- Official announcement! Broadcom will acquire VMware with us $61billion and assume US $8billion in debt
- Is there any risk in opening an account with Ping An Securities? Is it safe?
猜你喜欢

Leetcode 1984. 學生分數的最小差值(可以,已解决)

2022年GDCPC广东省大学生程序设计竞赛题解

【opencvsharpDNN】OpenCvSharp中YoloV3和Caffe的实现示例

< collection > and < Association > labels

杰理之蓝牙配网【篇】

【RK2206】4. Mqtt example

Leetcode 1984. 学生分数的最小差值(可以,已解决)

Download the latest global epidemic history data to the two days before the current day

HMI 创建工程生成字库的一个潜在bug

Unity UI scrollbar component
随机推荐
Common regular expressions
95後大廠程序員删庫被判刑9個月
How to view Tekton's pipeline indicators
安路科技发布SF1系列FPSoC新品:高集成低功耗,助力实现多种应用场景
2022 gdcpc Guangdong undergraduate program design competition
Jerry's camera frame number and layer [chapter]
Pyqt QWidget prohibits operations that are not current windows
【RK2206】4. MQTT示例
苹果宣布 2022 年 Apple 设计大奖得主
STM32 memory knowledge
asp.net txt读写
Not only Lenovo, which sells computers, but also what's new?
Unity UI scrollbar component
Shell script installing Prometheus and node_ exporter
杰理之修改video_buf_config.h的应用层视频缓冲区大小【篇】
[opencvsharpdnn] implementation example of yolov3 and Caffe in opencvsharp
护网面试(杂)
Neo4j桌面版数据库备份
Leetcode 1984. 學生分數的最小差值(可以,已解决)
Smart PLC calls the same subroutine (FC) multiple times