PHP - 如何从多个数组[重复]更改自动编号的键

时间:2022-01-07 03:07:18

This question already has an answer here:

这个问题在这里已有答案:

I have following multi dimensional array:

我有以下多维数组:

Array
(
    [0] => stdClass Object
        (
            [id] => 74
            [created_date] => 2015-12-10
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [1] => stdClass Object
        (
            [id] => 66
            [created_date] => 2015-12-10
            [instagram_id] => 481959735
            [total_comments_likes] => 70
            [username] => jack
        )

    [2] => stdClass Object
        (
            [id] => 837
            [created_date] => 2015-12-11
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [3] => stdClass Object
        (
            [id] => 829
            [created_date] => 2015-12-11
            [instagram_id] => 481959735
            [total_comments_likes] => 71
            [username] => jack
        )

    [4] => stdClass Object
        (
            [id] => 1601
            [created_date] => 2015-12-14
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [5] => stdClass Object
        (
            [id] => 1593
            [created_date] => 2015-12-14
            [instagram_id] => 481959735
            [total_comments_likes] => 71
            [username] => jack
        )

    [6] => stdClass Object
        (
            [id] => 1963
            [created_date] => 2015-12-15
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [7] => stdClass Object
        (
            [id] => 1955
            [created_date] => 2015-12-15
            [instagram_id] => 481959735
            [total_comments_likes] => 71
            [username] => jack
        )

    [8] => stdClass Object
        (
            [id] => 2279
            [created_date] => 2015-12-16
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [9] => stdClass Object
        (
            [id] => 2271
            [created_date] => 2015-12-16
            [instagram_id] => 481959735
            [total_comments_likes] => 71
            [username] => jack
        )

    [10] => stdClass Object
        (
            [id] => 4361
            [created_date] => 2015-12-18
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [11] => stdClass Object
        (
            [id] => 4353
            [created_date] => 2015-12-18
            [instagram_id] => 481959735
            [total_comments_likes] => 72
            [username] => jack
        )

    [12] => stdClass Object
        (
            [id] => 4401
            [created_date] => 2015-12-21
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [13] => stdClass Object
        (
            [id] => 4393
            [created_date] => 2015-12-21
            [instagram_id] => 481959735
            [total_comments_likes] => 72
            [username] => jack
        )

    [14] => stdClass Object
        (
            [id] => 5069
            [created_date] => 2015-12-22
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [15] => stdClass Object
        (
            [id] => 5061
            [created_date] => 2015-12-22
            [instagram_id] => 481959735
            [total_comments_likes] => 72
            [username] => jack
        )

    [16] => stdClass Object
        (
            [id] => 5770
            [created_date] => 2015-12-23
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [17] => stdClass Object
        (
            [id] => 5762
            [created_date] => 2015-12-23
            [instagram_id] => 481959735
            [total_comments_likes] => 72
            [username] => jack
        )

    [18] => stdClass Object
        (
            [id] => 6449
            [created_date] => 2015-12-24
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [19] => stdClass Object
        (
            [id] => 6441
            [created_date] => 2015-12-24
            [instagram_id] => 481959735
            [total_comments_likes] => 72
            [username] => jack
        )

    [20] => stdClass Object
        (
            [id] => 7047
            [created_date] => 2015-12-25
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [21] => stdClass Object
        (
            [id] => 7039
            [created_date] => 2015-12-25
            [instagram_id] => 481959735
            [total_comments_likes] => 72
            [username] => jack
        )

    [22] => stdClass Object
        (
            [id] => 7644
            [created_date] => 2015-12-28
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [23] => stdClass Object
        (
            [id] => 7636
            [created_date] => 2015-12-28
            [instagram_id] => 481959735
            [total_comments_likes] => 72
            [username] => jack
        )

    [24] => stdClass Object
        (
            [id] => 8250
            [created_date] => 2015-12-29
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [25] => stdClass Object
        (
            [id] => 8242
            [created_date] => 2015-12-29
            [instagram_id] => 481959735
            [total_comments_likes] => 73
            [username] => jack
        )

    [26] => stdClass Object
        (
            [id] => 8857
            [created_date] => 2015-12-30
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [27] => stdClass Object
        (
            [id] => 8849
            [created_date] => 2015-12-30
            [instagram_id] => 481959735
            [total_comments_likes] => 73
            [username] => jack
        )

    [28] => stdClass Object
        (
            [id] => 9462
            [created_date] => 2015-12-31
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [29] => stdClass Object
        (
            [id] => 9454
            [created_date] => 2015-12-31
            [instagram_id] => 481959735
            [total_comments_likes] => 73
            [username] => jack
        )

    [30] => stdClass Object
        (
            [id] => 10065
            [created_date] => 2016-01-05
            [instagram_id] => 12095962
            [total_comments_likes] => 16
            [username] => tom
        )

    [31] => stdClass Object
        (
            [id] => 10057
            [created_date] => 2016-01-05
            [instagram_id] => 481959735
            [total_comments_likes] => 73
            [username] => jack
        )

) 

and I am trying to regroup of my array by username and i have written this code:

我试图通过用户名重新组合我的数组,我写了这段代码:

$grouped = array();
foreach ($GetArrayOfEngagementPerInfluencer as $item) {
    $grouped[$item->username][] = $item;
}
p($grouped);

and got this result:

得到了这个结果:

Array
(
    [tom] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 74
                    [created_date] => 2015-12-10
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [1] => stdClass Object
                (
                    [id] => 837
                    [created_date] => 2015-12-11
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [2] => stdClass Object
                (
                    [id] => 1601
                    [created_date] => 2015-12-14
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [3] => stdClass Object
                (
                    [id] => 1963
                    [created_date] => 2015-12-15
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [4] => stdClass Object
                (
                    [id] => 2279
                    [created_date] => 2015-12-16
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [5] => stdClass Object
                (
                    [id] => 4361
                    [created_date] => 2015-12-18
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [6] => stdClass Object
                (
                    [id] => 4401
                    [created_date] => 2015-12-21
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [7] => stdClass Object
                (
                    [id] => 5069
                    [created_date] => 2015-12-22
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [8] => stdClass Object
                (
                    [id] => 5770
                    [created_date] => 2015-12-23
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [9] => stdClass Object
                (
                    [id] => 6449
                    [created_date] => 2015-12-24
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [10] => stdClass Object
                (
                    [id] => 7047
                    [created_date] => 2015-12-25
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [11] => stdClass Object
                (
                    [id] => 7644
                    [created_date] => 2015-12-28
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [12] => stdClass Object
                (
                    [id] => 8250
                    [created_date] => 2015-12-29
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [13] => stdClass Object
                (
                    [id] => 8857
                    [created_date] => 2015-12-30
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [14] => stdClass Object
                (
                    [id] => 9462
                    [created_date] => 2015-12-31
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

            [15] => stdClass Object
                (
                    [id] => 10065
                    [created_date] => 2016-01-05
                    [instagram_id] => 12095962
                    [total_comments_likes] => 16
                    [username] => tom
                )

        )

    [jack] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 66
                    [created_date] => 2015-12-10
                    [instagram_id] => 481959735
                    [total_comments_likes] => 70
                    [username] => jack
                )

            [1] => stdClass Object
                (
                    [id] => 829
                    [created_date] => 2015-12-11
                    [instagram_id] => 481959735
                    [total_comments_likes] => 71
                    [username] => jack
                )

            [2] => stdClass Object
                (
                    [id] => 1593
                    [created_date] => 2015-12-14
                    [instagram_id] => 481959735
                    [total_comments_likes] => 71
                    [username] => jack
                )

            [3] => stdClass Object
                (
                    [id] => 1955
                    [created_date] => 2015-12-15
                    [instagram_id] => 481959735
                    [total_comments_likes] => 71
                    [username] => jack
                )

            [4] => stdClass Object
                (
                    [id] => 2271
                    [created_date] => 2015-12-16
                    [instagram_id] => 481959735
                    [total_comments_likes] => 71
                    [username] => jack
                )

            [5] => stdClass Object
                (
                    [id] => 4353
                    [created_date] => 2015-12-18
                    [instagram_id] => 481959735
                    [total_comments_likes] => 72
                    [username] => jack
                )

            [6] => stdClass Object
                (
                    [id] => 4393
                    [created_date] => 2015-12-21
                    [instagram_id] => 481959735
                    [total_comments_likes] => 72
                    [username] => jack
                )

            [7] => stdClass Object
                (
                    [id] => 5061
                    [created_date] => 2015-12-22
                    [instagram_id] => 481959735
                    [total_comments_likes] => 72
                    [username] => jack
                )

            [8] => stdClass Object
                (
                    [id] => 5762
                    [created_date] => 2015-12-23
                    [instagram_id] => 481959735
                    [total_comments_likes] => 72
                    [username] => jack
                )

            [9] => stdClass Object
                (
                    [id] => 6441
                    [created_date] => 2015-12-24
                    [instagram_id] => 481959735
                    [total_comments_likes] => 72
                    [username] => jack
                )

            [10] => stdClass Object
                (
                    [id] => 7039
                    [created_date] => 2015-12-25
                    [instagram_id] => 481959735
                    [total_comments_likes] => 72
                    [username] => jack
                )

            [11] => stdClass Object
                (
                    [id] => 7636
                    [created_date] => 2015-12-28
                    [instagram_id] => 481959735
                    [total_comments_likes] => 72
                    [username] => jack
                )

            [12] => stdClass Object
                (
                    [id] => 8242
                    [created_date] => 2015-12-29
                    [instagram_id] => 481959735
                    [total_comments_likes] => 73
                    [username] => jack
                )

            [13] => stdClass Object
                (
                    [id] => 8849
                    [created_date] => 2015-12-30
                    [instagram_id] => 481959735
                    [total_comments_likes] => 73
                    [username] => jack
                )

            [14] => stdClass Object
                (
                    [id] => 9454
                    [created_date] => 2015-12-31
                    [instagram_id] => 481959735
                    [total_comments_likes] => 73
                    [username] => jack
                )

            [15] => stdClass Object
                (
                    [id] => 10057
                    [created_date] => 2016-01-05
                    [instagram_id] => 481959735
                    [total_comments_likes] => 73
                    [username] => jack
                )

        )

)

Every this perfect but I don't want to usename for key:

这一切都很完美,但我不想使用名字作为密钥:

Array
    (
        [tom] => Array
            (
                ...
            )
        [jack] => Array
            (
                ...
            )    
    )

Any idea how to change array key like this for better reading results:

知道如何更改这样的数组键以获得更好的读取结果:

Array
    (
        [0] => Array
            (
                ...
            )
        [1] => Array
            (
                ...
            )    
    )

3 个解决方案

#1


1  

Use array_values($array) function will return all values in an array with numerical index. reference: http://php.net/manual/en/function.array-values.php

使用array_values($ array)函数将返回具有数字索引的数组中的所有值。参考:http://php.net/manual/en/function.array-values.php

example:

例:

<?php
    $array = array("size" => "XL", "color" => "gold");
    print_r(array_values($array));
?>

output:

输出:

Array
(
    [0] => XL
    [1] => gold
)

#2


0  

Don't set it in your foreach:

不要在你的foreach中设置它:

foreach ($GetArrayOfEngagementPerInfluencer as $item) {
    $grouped[][] = $item;
}

#3


0  

How about you use $key as:

你怎么用$ key作为:

foreach ($GetArrayOfEngagementPerInfluencer as $key=>$item) {
    $grouped[$key][] = $item;
}

#1


1  

Use array_values($array) function will return all values in an array with numerical index. reference: http://php.net/manual/en/function.array-values.php

使用array_values($ array)函数将返回具有数字索引的数组中的所有值。参考:http://php.net/manual/en/function.array-values.php

example:

例:

<?php
    $array = array("size" => "XL", "color" => "gold");
    print_r(array_values($array));
?>

output:

输出:

Array
(
    [0] => XL
    [1] => gold
)

#2


0  

Don't set it in your foreach:

不要在你的foreach中设置它:

foreach ($GetArrayOfEngagementPerInfluencer as $item) {
    $grouped[][] = $item;
}

#3


0  

How about you use $key as:

你怎么用$ key作为:

foreach ($GetArrayOfEngagementPerInfluencer as $key=>$item) {
    $grouped[$key][] = $item;
}