【发布时间】:2021-01-08 16:50:25
【问题描述】:
我查看了merging recursive in inner arrays,但进展并不顺利。
我正在尝试通过将 cmets 与等于 comment_parent 的 comment_ID 嵌套来创建一个多维数组。
期望的结果:comment_id == comment_parent 的每条评论都应该嵌套在里面。
<?php
$comments = array(
array(
'comment_ID' => '4',
'comment_post_ID' => '1',
'comment_author' => 'cc',
'comment_author_email' => 'dev-email@flywheel.local',
'comment_author_url' => 'http://localhost:10004',
'comment_author_IP' => '127.0.0.1',
'comment_date' => '2021-01-08 15 =>59 =>28',
'comment_date_gmt' => '2021-01-08 15 =>59 =>28',
'comment_content' => 'Surely I am the second level comment in queue.',
'comment_approved' => '1',
'comment_parent' => '1',
'comment_karma' => '0',
'comment_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv =>84.0) Gecko/20100101 Firefox/84.0',
'comment_type' => 'comment',
'user_id' => '1'
),
array(
'comment_ID' => '3',
'comment_post_ID' => '1',
'comment_author' => 'cc',
'comment_author_email' => 'dev-email@flywheel.local',
'comment_author_url' => 'http://localhost:10004',
'comment_author_IP' => '127.0.0.1',
'comment_date' => '2021-01-08 15 =>58 =>38',
'comment_date_gmt' => '2021-01-08 15 =>58 =>38',
'comment_content' => 'I am third level comment',
'comment_approved' => '1',
'comment_parent' => '2',
'comment_karma' => '0',
'comment_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv =>84.0) Gecko/20100101 Firefox/84.0',
'comment_type' => 'comment',
'user_id' => '1'
),
array(
'comment_ID' => '2',
'comment_post_ID' => '1',
'comment_author' => 'cc',
'comment_author_email' => 'dev-email@flywheel.local',
'comment_author_url' => 'http://localhost:10004',
'comment_author_IP' => '127.0.0.1',
'comment_date' => '2021-01-08 15 =>49 =>52',
'comment_date_gmt' => '2021-01-08 15 =>49 =>52',
'comment_content' => 'I am a second level comment for the first one. Hello world comment.',
'comment_approved' => '1',
'comment_parent' => '1',
'comment_karma' => '0',
'comment_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv =>84.0) Gecko/20100101 Firefox/84.0',
'comment_type' => 'comment',
'user_id' => '1'
),
array(
'comment_ID' => '1',
'comment_post_ID' => '1',
'comment_author' => 'A WordPress Commenter',
'comment_author_email' => 'wapuu@wordpress.example',
'comment_author_url' => 'https =>//wordpress.org/',
'comment_author_IP' => '',
'comment_date' => '2021-01-02 13 =>03 =>52',
'comment_date_gmt' => '2021-01-02 13 =>03 =>52',
'comment_content' => 'Update, Hi, this is a comment.\r\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\r\nCommenter avatars come from <a href=\'https =>//gravatar.com\' rel=\'nofollow ugc\'>Gravatar</a>.',
'comment_approved' => '1',
'comment_parent' => '0',
'comment_karma' => '0',
'comment_agent' => '',
'comment_type' => 'comment',
'user_id' => '0'
)
);
$grouped = [];
// $arr is your initial array
array_walk($comments, function($v) use (&$grouped){
if (array_key_exists($v['comment_ID'], $grouped)) {
$grouped[$v['comment_ID']]['comment_parent'][] = $v['comment_parent'];
} else {
$v['comment_parent'] = [$v['comment_parent']];
$grouped[$v['comment_ID']] = $v;
}
});
return $grouped;
期望的结果:comment_id == comment_parent 的每条评论都应该嵌套在里面。
我需要 cmets { 评论 1 { 回复评论一 { 回复评论一的评论 } } 评论 2 { 回复评论二 { 回复评论二的评论 } }
回复与评论 ID 具有相同的父 ID。
<?php
$result = array(
array(
// Comments with key comment_post_ID value 1 or 2 ... level one
'1' => array(
// comments with key comment_parent value 1 for example next level
'1' => array(
array(
// details inset.
'comment_ID' => '4',
'comment_author' => 'cc',
'comment_author_email' => 'dev-email@flywheel.local',
'comment_author_url' => 'http://localhost:10004',
'comment_author_IP' => '127.0.0.1',
'comment_date' => '2021-01-08 15 =>59 =>28',
'comment_date_gmt' => '2021-01-08 15 =>59 =>28',
'comment_content' => 'Surely I am the second level comment in queue.',
'comment_approved' => '1',
'comment_type' => 'comment',
'user_id' => '1'
),
array(
// details inset.
'comment_ID' => '3',
'comment_author' => 'cc',
'comment_author_email' => 'dev-email@flywheel.local',
'comment_author_url' => 'http://localhost:10004',
'comment_author_IP' => '127.0.0.1',
'comment_date' => '2021-01-08 15 =>59 =>28',
'comment_date_gmt' => '2021-01-08 15 =>59 =>28',
'comment_content' => 'Surely I am the second level comment in queue.',
'comment_approved' => '1',
'comment_type' => 'comment',
'user_id' => '1'
),
)
)
)
);
【问题讨论】:
-
请添加所需输出的简短示例
-
@syscall 我已经解决了这个问题。