【问题标题】:Average columns in PHPPHP 中的平均列数
【发布时间】:2017-06-07 23:23:58
【问题描述】:

我有一系列列(1 月、2 月、3 月等),我想对每一行的每一列的值进行平均,无论有多少列。

我有:

protected function generateAverage($staff, $type)
{
    $months = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    $staff = array_map(function ($row) use ($type) {
        if ($row['row_type'] == $type) {
            return $row;
        }
    }, $staff);

    foreach ($staff as $key => $employee) {
        $months[0] += $employee['amounts'][0];
        $months[1] += $employee['amounts'][1];
        $months[2] += $employee['amounts'][2];
        $months[3] += $employee['amounts'][3];
        $months[4] += $employee['amounts'][4];
        $months[5] += $employee['amounts'][5];
        $months[6] += $employee['amounts'][6];
        $months[7] += $employee['amounts'][7];
        $months[8] += $employee['amounts'][8];
        $months[9] += $employee['amounts'][9];
        $months[10] += $employee['amounts'][10];
        $months[11] += $employee['amounts'][11];
    }
    $months = array_map(function ($value) use ($staff) {
        return $value / (count($staff) / 2);
    }, $months);
    $months[] = array_sum($months);
    return $months;
}

这是进入上述函数的数据示例:

array:6 [
  0 => array:4 [
    "amounts" => array:13 [
      0 => "30000.00"
      1 => "30000.00"
      2 => "30000.00"
      3 => "30000.00"
      4 => "30000.00"
      5 => "30000.00"
      6 => "30000.00"
      7 => "30000.00"
      8 => "30000.00"
      9 => "30000.00"
      10 => "30000.00"
      11 => "30000.00"
      12 => 360000.0
    ]
    "image" => "test.jpg"
    "row_name" => "Target"
    "row_type" => "target"
  ]
...

用法:

$data['aggregates']['Target average'] = $this->generateAverage(array_values($data['staff']), 'target');

我觉得计算平均值的方式很混乱,有没有更好的方法来做到这一点?

【问题讨论】:

    标签: php arrays laravel laravel-4


    【解决方案1】:

    由于您使用的是 Laravel,因此您使用的大部分数据都是集合。因此,在将集合转换为数组之前,您可以使用avg() helper:

    $collection->avg($key);
    

    【讨论】:

    • 他想要对跨越多条记录的特定数据进行平均。我不认为任何 avg 函数可以神奇地弄清楚他想做什么。
    • @NDM 看起来他从 DB 中获取数据,在这种情况下,最好使用查询生成器的 avg() 助手或集合 avg() 助手。
    【解决方案2】:

    一些小的占用空间减少

    protected function generateAverage($staff, $type)
    {
        // create 12 months with 0 value
        $months = array_fill(0, 12, 0);
        // use array_filter instead of map
        $staff = array_filter(function ($row) use ($type) {
            return $row['row_type'] === $type;
        }, $staff);
        // do count outside loop
        $staffCount = count($staff);
        // loop employees and add up each month, dividing early
        foreach ($staff as $employee) {
            for ($i = 0; $i < 12; $i++) {
                $months[$i] += $employee['amounts'][$i] / $staffCount;
            }
        }
        return $months;
    }
    

    我不知道你为什么要将员工人数除以 2 或为什么最后要求和,我的函数只是给出每月的平均值。

    【讨论】:

      【解决方案3】:

      我觉得你可以考虑使用array_colum,

      1) array_column 将行中特定索引位置的所有值组成一个数组

      $column1 =  array_column($employee, 0 );
      $column2 =  array_column($employee, 1 );
      $column3 =  array_column($employee, 2 );
      .
      .
      .
      

      2)通过count($columnX)获取列数,其中X是列的索引

      3) 根据需要使用 (1) 和 (2) 计算平均值

      【讨论】:

        猜你喜欢
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-13
        • 1970-01-01
        • 2017-01-19
        • 1970-01-01
        相关资源
        最近更新 更多