【问题标题】:PHP Group single Array into a multidimentional arrayPHP将单个数组分组为多维数组
【发布时间】:2021-01-08 16:50:25
【问题描述】:

我查看了merging recursive in inner arrays,但进展并不顺利。

我正在尝试通过将 cmets 与等于 comment_parentcomment_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 我已经解决了这个问题。

标签: php arrays


【解决方案1】:

您可以使用 foreach 并使用键 [comment_post_ID] 和 [comment_parent] 添加到 $grouped 数组:

$comments = [
    ['comment_ID' => '4', 'comment_post_ID' => '1', 'comment_parent' => '1', /*other data*/ ],
    ['comment_ID' => '3', 'comment_post_ID' => '1', 'comment_parent' => '2', /*other data*/ ],
    ['comment_ID' => '2', 'comment_post_ID' => '1', 'comment_parent' => '1', /*other data*/ ],
    ['comment_ID' => '1', 'comment_post_ID' => '1', 'comment_parent' => '0', /*other data*/ ],
];

$grouped = [];
foreach ($comments as $comment)
{
    // shortcuts for readability
    $post_id = $comment['comment_post_ID'];
    $parent_id = $comment['comment_parent'];

    // Comments with key comment_post_ID value 1 or 2 ... level one
    // the, comments with key comment_parent value 1 for example next level
    $grouped[$post_id][$parent_id][] = $comment;
}

print_r($grouped);

输出:

Array (
    [1] => Array (
            [1] => Array (
                    [0] => Array (
                            [comment_ID] => 4
                            [comment_post_ID] => 1
                            [comment_parent] => 1
                        )
                    [1] => Array (
                            [comment_ID] => 2
                            [comment_post_ID] => 1
                            [comment_parent] => 1
                        )
                )
            [2] => Array (
                    [0] => Array (
                            [comment_ID] => 3
                            [comment_post_ID] => 1
                            [comment_parent] => 2
                        )
                )
            [0] => Array (
                    [0] => Array (
                            [comment_ID] => 1
                            [comment_post_ID] => 1
                            [comment_parent] => 0
                        )
                )
        )
)

使用您问题的(缩短的)数据。

【讨论】:

  • 抱歉回复晚了。这可以将 cmets 嵌套到 post_id 中,但不尊重深度级别。我已编辑问题以使其更好。
  • 期望结果:comment_id == comment_parent 的每条评论都应该嵌套在里面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
相关资源
最近更新 更多