简体   繁体   中英

PHP Group single Array into a multidimentional array

I have looked at merging recursive in inner arrays but the is not going so well.

I am trying to get the array below to make a multidimensional array by nesting comments with comment_ID that is equal to comment_parent .

Desired result: Each comment with the comment_id == comment_parent should nest inside.

<?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;

Desired result: Each comment with the comment_id == comment_parent should nest inside.

I need to have comments { comment 1 { Reply to comment one { Reply to comment of comment one } } comment 2 { Reply to comment two { Reply to comment of comment two } } }

a reply simply has the same parent ID as the comment 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'
                ),
            )
        )
    )
);

You could use a foreach and add to the $grouped array, using the keys [comment_post_ID] and [comment_parent]:

$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);

Output:

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
                        )
                )
        )
)

using your question's (shortened) data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM