2017-01-23 18 views
0

私は一連のカラム(Jan、Feb、Marなど)を持っていますが、各行の各カラムの値を平均したいと思います。PHPの平均カラム

私がしている:

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

私は道平均を感じます計算が面倒です、これを行うには良い方法がありますか?

答えて

1

私はあなたが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; 
} 

最後に合計している、私の関数はちょうど1ヶ月あたりの平均を与える。

0

Laravelを使用しているので、あなたが扱うデータのほとんどはコレクションです。だから、配列にコレクションを変換する前に、あなたはavg() helperを使用することができます。

$collection->avg($key); 
+0

必要に応じて、(2)平均値を計算する場合、彼は、特定のデータの平均値を作りたいです複数のレコードにまたがる私はどんな平均的な機能も魔法のように彼が何をしたいのか理解できないと思います。 – NDM

+0

@NDM DBからデータを取得するように見えますが、この場合、クエリビルダーの 'avg()'ヘルパーまたはコレクション 'avg()'ヘルパーを使う方が良いでしょう。 –

0
私はあなたがarray_columの使用を検討することができると思う

1)array_columnが行

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

2)count($columnX)によって列数を取得するには、特定のインデックス位置にあるすべての値の配列を作ります、 X(1)を使用)カラム

3の指標であり、