本篇文章主要介绍了"PHP模拟SQL的GROUP BY算法",主要涉及到方面的内容,对于PHPjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
BY JENNER · 2015年1月24日· 阅读次数:25github地址:https://github.com/huyanping/Zebra-PHP-A...
BY JENNER · 2015年1月24日· 阅读次数:25
github地址:https://github.com/huyanping/Zebra-PHP-ArrayGroupBy
packagist地址:https://packagist.org/packages/jenner/array_group_by为什么使用Zebra-PHP-ArrayGroupBy
在如下场景中,我们总是希望能够在php中使用类似mysql的groupby操作:SQL过于复杂,造成数据库运算效率低下从数据库中读取出原始数据,在php中进行运算,增强代码重用率其他非数据库场景的数组归并场景Zebar-PHP-ArrayGroupBy能够做什么
对二维数组进行归并归并的同时,支持对字段进行自定义处理比SQL更灵活的自定义函数,你可以随意编写归并和字段合并函数示例:$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){returndate('Y', strtotime($value)); }];$group_by_value= ['order_date'=> ['callback'=>function($value_array){returnsubstr($value_array[0], 0, 4); },'as'=>'year' ],'price'=>function($value_array){returnarray_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){returnsubstr($field, 0, 10); }];$group_by_values= ['bill_time'=>function($field_values){returnsubstr($field_values[0], 0, 10); },'price'=>function($field_values){returnarray_sum($field_values); },'cnt'=>function($field_values){returnarray_sum($field_values); }];$week_fields= ['bill_time'=>function($field){returndate('w', strtotime($field)); }];$week_values= ['bill_time'=>function($field_values){returndate('w', strtotime($field_values[0])); },'price'=>function($field_values){returnarray_sum($field_values); },'cnt'=>function($field_values){returnarray_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);举例归并过程中,实现对结果的中值计算归并过程中,对时间字段进行自定义处理,例如归并每5分钟的数据等等原创文章,转载请注明: 转载自始终不够
本文链接地址: PHP模拟SQL的GROUP
BY算法
以上就介绍了PHP模拟SQL的GROUP BY算法,包括了方面的内容,希望对PHPjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_109023.html