如何用php对键组合多维数组?

时间:2013-10-07 07:50:31

标签: php multidimensional-array

我有这个数组:

Array
(
    [0] => Array
        (
            [0] => 78
            [mount] => 78
            [1] => activation
            [type] => activation
            [2] => 2013-10-01
            [insertdate] => 2013-10-01
        )

    [1] => Array
        (
            [0] => 130
            [mount] => 130
            [1] => activation
            [type] => activation
            [2] => 2013-10-02
            [insertdate] => 2013-10-02
        )

    [2] => Array
        (
            [0] => 210
            [mount] => 210
            [1] => activation
            [type] => activation
            [2] => 2013-10-03
            [insertdate] => 2013-10-03
        )

    [3] => Array
        (
            [0] => 190
            [mount] => 190
            [1] => activation
            [type] => activation
            [2] => 2013-10-04
            [insertdate] => 2013-10-04
        )

    [4] => Array
        (
            [0] => 250
            [mount] => 250
            [1] => activation
            [type] => activation
            [2] => 2013-10-05
            [insertdate] => 2013-10-05
        )

    [5] => Array
        (
            [0] => 300
            [mount] => 300
            [1] => activation
            [type] => activation
            [2] => 2013-10-06
            [insertdate] => 2013-10-06
        )

    [6] => Array
        (
            [0] => 330
            [mount] => 330
            [1] => activation
            [type] => activation
            [2] => 2013-10-07
            [insertdate] => 2013-10-07
        )

    [7] => Array
        (
            [0] => 100
            [mount] => 100
            [1] => revenue
            [type] => revenue
            [2] => 2013-10-01
            [insertdate] => 2013-10-01
        )

    [8] => Array
        (
            [0] => 310
            [mount] => 310
            [1] => revenue
            [type] => revenue
            [2] => 2013-10-02
            [insertdate] => 2013-10-02
        )

    [9] => Array
        (
            [0] => 200
            [mount] => 200
            [1] => revenue
            [type] => revenue
            [2] => 2013-10-03
            [insertdate] => 2013-10-03
        )

    [10] => Array
        (
            [0] => 400
            [mount] => 400
            [1] => revenue
            [type] => revenue
            [2] => 2013-10-04
            [insertdate] => 2013-10-04
        )

    [11] => Array
        (
            [0] => 470
            [mount] => 470
            [1] => revenue
            [type] => revenue
            [2] => 2013-10-05
            [insertdate] => 2013-10-05
        )

    [12] => Array
        (
            [0] => 310
            [mount] => 310
            [1] => revenue
            [type] => revenue
            [2] => 2013-10-06
            [insertdate] => 2013-10-06
        )

    [13] => Array
        (
            [0] => 600
            [mount] => 600
            [1] => revenue
            [type] => revenue
            [2] => 2013-10-07
            [insertdate] => 2013-10-07
        )

)

目标是将其转换为1维数组,结果为:

array([type]=>activation [mount]=>78,130,210,190,250,300,330),
array([type]=>revenue [mount]=>100,310,200,400,470,310,600)

2 个答案:

答案 0 :(得分:0)

使用PHP4和更新版本有一种非常简单的方法,该函数称为array_merge_recursive()。它将子数组合并为一个。如果有相同的键,则将其合并为一个键,并将数组作为值。

请参阅完整文档here

答案 1 :(得分:0)

我认为这不是最好的变体,但无论如何,它是有效的

    foreach ($array as $value) {
        $array2[$value['type']][] = $value['mount'];
    }
    foreach ($array2 as $key => $value) {
        $result[] = array(
            'type' => $key,
            'mount' => implode(',', $value)
        );
    }