我在PHP中有下面的數組。 Id和Total鍵對於相同的Id值是相同的。在PHP中合併具有相同ID值的數組元素
Array
(
[0] => Array
(
[id] => 1
[total] => 100
[line] => books
[value] => 10
)
[1] => Array
(
[id] => 1
[total] => 100
[line] => cars
[value] => 50
)
[2] => Array
(
[id] => 1
[total] => 100
[line] => cover
[value] => 30
)
[3] => Array
(
[id] => 2
[total] => 80
[line] => books
[value] => 5
)
[4] => Array
(
[id] => 2
[total] => 80
[line] => cover
[value] => 35
)
[5] => Array
(
[id] => 2
[total] => 80
[line] => cars
[value] => 40
)
)
我想合併數組內線路和Value元素。 爲了說明我看到的結果是下面的數組。
Array
(
[0] => Array
(
[id] => 1
[total] => 100
[details] => Array
(
[0] => Array
(
[line] => books
[value] => 10
)
[1] => Array
(
[line] => cars
[value] => 50
)
[2] => Array
(
[line] => cover
[value] => 30
)
)
)
)
[1] => Array
(
[id] => 2
[total] => 80
[details] => Array
(
[0] => Array
(
[line] => books
[value] => 5
)
[1] => Array
(
[line] => cars
[value] => 35
)
[2] => Array
(
[line] => cover
[value] => 40
)
)
)
)
我正在使用PHP。謝謝你的幫助。
簡單的foreach循環將工作 - 你需要在最少嘗試一下 – 2015-11-03 23:55:00
歡迎來到Stack Overflow。請發佈您迄今爲止的嘗試。 – Twisty