3

我有一个 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)
)
4

1 回答 1

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

似乎做得很好。

于 2013-05-18T14:36:35.263 回答