我们的软件以关联数组的形式提供购买的产品列表。我们需要组合这个数组并创建一个具有合并计数的新数组。然后,该数组将用于为我们的计费部门创建采购清单。
样本数组:
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 循环会很慢,而且可以做得更有效率。