我們的軟件在關聯數組中提供購買的產品列表。我們需要組合這個數組並創建一個具有統一計數的新數組。然後,此陣列將用於爲我們的結算部門創建採購清單。PHP組合並計數關聯數組
樣本數組:
Array
(
[0] => Array
(
[item] => Long Sleeve-Shirt & Hooded Sweatshirt
[quantity] => 1
[size] => Youth Small
[color] => Blue
)
[1] => Array
(
[item] => Long Sleeve-Shirt & Hooded Sweatshirt
[quantity] => 1
[size] => Adult Small
[color] => Red
)
[2] => Array
(
[item] => Hooded Sweatshirt Youth & Adult Sizes
[quantity] => 1
[size] => Youth Large
[color] => Blue
)
[3] => Array
(
[item] => Long Sleeve-Shirt & Hooded Sweatshirt
[quantity] => 1
[size] => Youth Small
[color] => Blue
)
}
綜合陣列
Array
(
[0] => Array
(
[item] => Long Sleeve-Shirt & Hooded Sweatshirt
[quantity] => 2
[size] => Youth Small
[color] => Blue
)
[1] => Array
(
[item] => Long Sleeve-Shirt & Hooded Sweatshirt
[quantity] => 1
[size] => Adult Small
[color] => Red
)
[2] => Array
(
[item] => Hooded Sweatshirt Youth & Adult Sizes
[quantity] => 1
[size] => Youth Large
[color] => Blue
)
}
我將如何創建一個函數來第一陣列下結合,創建第二個陣列。我能想到的唯一的事情就是創建幾個foreach循環,並慢慢打破列表。我沒有使用關聯數組和函數,我相信做多個foreach循環會很慢,並且可以做得更有效率。
這很好。你試過什麼了? –
有沒有問題?人們怎麼回答?我可以幫你解決,但我不明白你想做什麼。 – Ms01