当前位置:网站首页>学习使用php实现无限极评论和无限极转二级评论解决方案
学习使用php实现无限极评论和无限极转二级评论解决方案
2022-06-24 09:40: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_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] => 回复
)
)
)
)
边栏推荐
- CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算
- Why is JSX syntax so popular?
- 操作符详解
- 2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
- Honeypot 2 hfish, ehoney
- 桌面软件开发框架大赏
- 队列Queue
- 机器学习——主成分分析(PCA)
- LeetCode: 240. Search 2D matrix II
- 如何提高网络基础设施排障效率,告别数据断档?
猜你喜欢

How to standardize data center infrastructure management process

Groovy obtains Jenkins credentials through withcredentials

canvas 绘制图片

415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)

Cookie encryption 4 RPC method determines cookie encryption

ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?

记录一下MySql update会锁定哪些范围的数据

JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战

生产者/消费者模型

英伟达这篇CVPR 2022 Oral火了!2D图像秒变逼真3D物体!虚拟爵士乐队来了!
随机推荐
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
Handling method of Oracle data file header SCN inconsistency
How large and medium-sized enterprises build their own monitoring system
Troubleshooting steps for Oracle pool connection request timeout
Baidu AI template for knowledge understanding
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
分布式 | 如何与 DBLE 进行“秘密通话”
Go language development environment setup +goland configuration under the latest Windows
引擎国产化适配&重构笔记
[db2] sql0805n solution and thinking
vim的使用
el-table点击添加行样式
SQL Server AVG函数取整问题
YOLOv6:又快又准的目标检测框架开源啦
415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
Queue queue
oracle池式连接请求超时问题排查步骤
生产者/消费者模型
美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
How to standardize data center infrastructure management process