我对数组有问题。我要总结给定的数量(数量)。
我想得到:
blyszczaca Normal = 6;
czapkoszal正常= 6;
czapkoszal Luxury = 2;
etola狼= 1;
etola正常= 2;
此数组中的。你能帮我吗?我考虑了最近4个小时,什么也没有。还在学习。
Array
(
[0] => Array
(
[produkt] => blyszczaca
[model] => Normal
[price] => 27.00
[qty] => 2
)
[1] => Array
(
[0] => blyszczaca
[1] => Normal
[2] => 27.00
[3] => 2
)
[2] => Array
(
[0] => blyszczaca
[1] => Normal
[2] => 27.00
[3] => 2
)
[3] => Array
(
[0] => czapkoszal
[1] => Normal
[2] => 41.00
[3] => 2
)
[4] => Array
(
[0] => czapkoszal
[1] => Luxury
[2] => 45.00
[3] => 2
)
[5] => Array
(
[0] => czapkoszal
[1] => Normal
[2] => 41.00
[3] => 2
)
[6] => Array
(
[0] => czapkoszal
[1] => Normal
[2] => 41.00
[3] => 1
)
[7] => Array
(
[0] => czapkoszal
[1] => Normal
[2] => 41.00
[3] => 1
)
[8] => Array
(
[0] => etola
[1] => Wolf
[2] => 47.00
[3] => 1
)
[9] => Array
(
[0] => etola
[1] => Normal
[2] => 39.00
[3] => 1
)
[10] => Array
(
[0] => etola
[1] => Normal
[2] => 39.00
[3] => 1
)
)
谢谢。
答案 0 :(得分:2)
您应该使用唯一的键创建新的数组并计算数量,例如:
$sums = [];
foreach ($your_array as $item) {
// create a unique key as concatenation of `product` and `model`
$key = $item['product'] . ':' . $item['model'];
// check if such key exists, if not - init key with `0`
if (!isset($sums[$key])) {
$sums[$key] = 0;
}
// add current `qty` to the value of `$sums[$key]`
$sums[$key] += $item['qty']
}
答案 1 :(得分:0)
您可以在输出数组中基于produkt
和model
创建自定义键。然后,将qty
添加到该键。请尝试以下操作:
// Create a array to store the sum
$sums = array();
// Loop over the input array
foreach ($input as $value) {
// create a custom key based on produkt and model
$key = $value['produkt'] . ' ' . $value['model'];
// initalize the key
$sums[$key] = $sums[$key] ?? 0;
// add the quantity
$sums[$key] += (int)$value['qty'];
}