转换PHP数组

时间:2013-05-18 14:34:45

标签: php arrays

我有一个PHP数组,我正在尝试将其转换为不同的格式,我能想到的唯一方法就是它应该需要更长的时间。

示例数据:

Array (
[0] => Array (
    [type] => a
    [value] => 1
    )
[1] => Array (
    [type] => a
    [value] => 2
    )
[2] => Array (
    [type] => a
    [value] => 3
    )
[3] => Array (
    [type] => b
    [value] => 1
    )
[4] => Array (
    [type] => b
    [value] => 4
    )
[5] => Array (
    [type] => f
    [value] => 2
    )
) 

分为:

Array (
'a' => Array(1,2,3),
'b' => Array(1,4),
'f' => Array(2)
)

1 个答案:

答案 0 :(得分:10)

foreach ($array as $element) 
    $newArray[$element["type"]][] = $element["value"];

Seems to do the trick rather nicely.