BY JENNER · 2015 年 1 月 24 日 · 読み取り数: 25
github アドレス: https://github.com/huyanping/Zebra-PHP-ArrayGroupBy
packagegist アドレス: https://packagist .org/packages/jenner/array_group_by
Zebra-PHP-ArrayGroupBySQL が複雑すぎるため、データベース操作が発生する 低効率性
$records = [ ['order_date' => '2014-01-01', 'price' => 5], ['order_date' => '2014-01-02', 'price' => 10], ['order_date' => '2014-01-03', 'price' => 20], ['order_date' => '2015-01-04', 'price' => 25],];$group_by_fields = [ 'order_date' => function($value){ return date('Y', strtotime($value)); }];$group_by_value = [ 'order_date' => [ 'callback' => function($value_array){ return substr($value_array[0], 0, 4); }, 'as' => 'year' ], 'price' => function($value_array){ return array_sum($value_array); },];$grouped = \Jenner\Zebra\ArrayGroupBy::groupBy($records, $group_by_fields, $group_by_value);print_r($grouped);
結果:
Array( [0] => Array ( [year] => 2014 [price] => 35 ) [1] => Array ( [year] => 2015 [price] => 25 ))
チェーンも使用できますメソッドを呼び出してデータを複数回要約すると、より柔軟になります:
$records = [ ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-02 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-02 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-02 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-02 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-02 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-04 00:00:00', 'price'=>1, 'cnt'=>3,], ['bill_time'=>'2014-01-04 00:00:00', 'price'=>1, 'cnt'=>3,],];$group_by_fields = [ 'bill_time' => function($field){ return substr($field, 0, 10); }];$group_by_values = [ 'bill_time' => function($field_values){ return substr($field_values[0], 0, 10); }, 'price' => function($field_values){ return array_sum($field_values); }, 'cnt' => function($field_values){ return array_sum($field_values); }];$week_fields = [ 'bill_time' => function($field){ return date('w', strtotime($field)); }];$week_values = [ 'bill_time' => function($field_values){ return date('w', strtotime($field_values[0])); }, 'price' => function($field_values){ return array_sum($field_values); }, 'cnt' => function($field_values){ return array_sum($field_values); }];$grouped = (new \Jenner\Zebra\ArrayGroupBy($records))->groupByField($group_by_fields)->groupByValue($group_by_values)->groupByField($week_fields)->groupByValue($week_values)->get();print_r($grouped);
例
マージ プロセス中に、結果の中央値の計算が実現されます