【问题标题】:Find the average price for every product type in array查找数组中每种产品类型的平均价格
【发布时间】:2017-05-22 18:32:25
【问题描述】:

所以我有这个数组:

$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)
);

我需要找出每种产品类型的平均价格,例如:
(盒子手套 + 空手道手套 + mma 手套)/ 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”数字。

【问题讨论】:

  • array_filter 和 array_reduce 是你的朋友。

标签: php arrays count average


【解决方案1】:

这里是一个没有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
]

【讨论】:

    【解决方案2】:

    您无需对价格求和,只需将它们添加到数组中即可。

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

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

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

    【讨论】:

      【解决方案3】:

      使用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
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-29
        • 2020-07-11
        • 2020-03-05
        • 1970-01-01
        相关资源
        最近更新 更多