查找阵列中每种产品类型的平均价格

时间:2017-05-22 18:32:26

标签: php arrays count average

所以我有这个数组:

$arr = array(
    array('category' => 'box', 'product' => 'gloves', 'quantity' => 45, 'price' => 30),
    array('category' => 'karate', 'product' => 'gloves', 'quantity' => 55, 'price' => 25),
    array('category' => 'mma', 'product' => 'gloves', 'quantity' => 55, 'price' => 35),
    array('category' => 'mma', 'product' => 'shorts', 'quantity' => 120, 'price' => 50),
    array('category' => 'karate', 'product' => 'belt', 'quantity' => 25, 'price' => 55),
    array('category' => 'judo', 'product' => 'costume', 'quantity' => 30, 'price' => 90),
    array('category' => 'karate', 'product' => 'belt', 'quantity' => 25, 'price' => 90)
);

我需要找出每种产品类型的平均价格,例如:
(箱式手套+空手道手套+手套)/ 3 =(30 + 25 + 35)/ 3 = 30。

这是我现在的代码,它输出一个数组,其中包含每种产品类型的总和:

$tmp = array();
foreach($arr as $key => $value) {

    $tmp[$arr[$key]['product']] = isset($tmp[$arr[$key]['product']]) ? $tmp[$arr[$key]['produs']]: 0;

    $tmp[$arr[$key]['product']] = $tmp[$arr[$key]['product']] + $arr[$key]['price'];     

}

输出:

[gloves] => 90
[shorts] => 50
[costume] => 90
[belt] => 145

我如何找到每种产品类型的计数?

例如对于手套,它将是90/3,“3”号码我不知道如何获得它。

3 个答案:

答案 0 :(得分:0)

不要随意汇总价格,只需将它们添加到数组中即可。

foreach ($arr as $item) {
    $products[$item['product']][] = $item['price'];
}

然后您可以计算结果组的平均值。

$products = array_map(function($product){
    return array_sum($product) / count($product);
}, $products);

var_dump($products);

答案 1 :(得分:0)

使用array_reducearray_map函数的解决方案:

$result = array_map(function($v){
    $c = count($v);
    return ($c > 1)? array_sum($v)/$c : $v[0];
}, array_reduce($arr, function($r, $a){
    $r[$a['product']][] = $a['price'];  // accumulating prices for each `product`
    return $r;
}, []));

print_r($result);

输出:

Array
(
    [gloves] => 30
    [shorts] => 50
    [belt] => 72.5
    [costume] => 90
)

答案 2 :(得分:0)

以下是没有arrray functions的示例:

$arr = array(
    array('category' => 'box', 'product' => 'gloves', 'quantity' => 45, 'price' => 30),
    array('category' => 'karate', 'product' => 'gloves', 'quantity' => 55, 'price' => 25),
    array('category' => 'mma', 'product' => 'gloves', 'quantity' => 55, 'price' => 35),
    array('category' => 'mma', 'product' => 'shorts', 'quantity' => 120, 'price' => 50),
    array('category' => 'karate', 'product' => 'belt', 'quantity' => 25, 'price' => 55),
    array('category' => 'judo', 'product' => 'costume', 'quantity' => 30, 'price' => 90),
    array('category' => 'karate', 'product' => 'belt', 'quantity' => 25, 'price' => 90)
);

$tmp = array();
foreach ($arr as $value) {
    if (isset($tmp[$value['product']])) {
        $tmp[$value['product']]['total'] += $value['price'];
        $tmp[$value['product']]['price'] += 1;
    } else {
        $tmp[$value['product']]['total'] = $value['price'];
        $tmp[$value['product']]['price'] = 1;
    }
}

foreach ($tmp as $key => $value) {
    $tmp[$key] = $value['total'] / $value['price'];
}

$tmp值:

[
    'gloves' => (int) 30,
    'shorts' => (int) 50,
    'belt' => (float) 72.5,
    'costume' => (int) 90
]