0

我有这个数组:

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
        )

)

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

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

2 回答 2

0

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

    foreach ($array as $value) {
        $array2[$value['type']][] = $value['mount'];
    }
    foreach ($array2 as $key => $value) {
        $result[] = array(
            'type' => $key,
            'mount' => implode(',', $value)
        );
    }
于 2013-10-07T08:05:52.873 回答
0

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

请参阅此处的完整文档。

于 2013-10-07T07:55:14.197 回答