用数字索引对php数组进行分组

时间:2016-03-30 15:45:23

标签: php arrays grouping

我想像这样分组php多维数组

Array
(
[0] => Array
    (
        [0] => Array
            (
                [DATEDAILYRPT] => Mar  1 2016 12:00:00:000AM
                [TODAY] => 17043140.0000
            )

        [1] => Array
            (
                [DATEDAILYRPT] => Mar  1 2016 12:00:00:000AM
                [TODAY] => 3805785.0000
            )

        [2] => Array
            (
                [DATEDAILYRPT] => Mar  1 2016 12:00:00:000AM
                [TODAY] => 49587.0000
            )


    )

[1] => Array
    (
        [0] => Array
            (
                [DATEDAILYRPT] => Mar  2 2016 12:00:00:000AM
                [TODAY] => 16484380.0000
            )

        [1] => Array
            (
                [DATEDAILYRPT] => Mar  2 2016 12:00:00:000AM
                [TODAY] => 27123966.0000
            )

        [2] => Array
            (
                [DATEDAILYRPT] => Mar  2 2016 12:00:00:000AM
                [TODAY] => 50413.0000
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [DATEDAILYRPT] => Mar  3 2016 12:00:00:000AM
                [TODAY] => 16410000.0000
            )

        [1] => Array
            (
                [DATEDAILYRPT] => Mar  3 2016 12:00:00:000AM
                [TODAY] => 29247107.0000
            )

        [2] => Array
            (
                [DATEDAILYRPT] => Mar  3 2016 12:00:00:000AM
                [TODAY] => -20661.0000
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [DATEDAILYRPT] => Mar  4 2016 12:00:00:000AM
                [TODAY] => 18459587.0000
            )

        [1] => Array
            (
                [DATEDAILYRPT] => Mar  4 2016 12:00:00:000AM
                [TODAY] => 30079338.0000
            )

        [2] => Array
            (
                [DATEDAILYRPT] => Mar  4 2016 12:00:00:000AM
                [TODAY] => 0.0000
            )

    )
)

我需要将所有0个子索引分组为0,1个子索引和1等。 例如,我想将0 => 0分组,其中1 => 0,2 => 0和3 => 0。也0 => 1,1 = 1> 1,2 => 1且3 => 1等等。

1 个答案:

答案 0 :(得分:0)

如果您的PHP版本是> = 5.5,则可以使用array_column

for ($i = 0; $i < 3; $i++) {
   $groups[] = array_column($your_array, $i);
}

如果您的PHP版本较旧并且您没有array_column,则可以使用这样的嵌套foreach循环:

foreach ($your_array as $day) {
    foreach ($day as $key => $value) {
        $groups[$key][] = $value;
    }
}