国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 邏輯算法 > 正文

PHP模擬SQL的GROUPBY算法

2020-03-22 19:29:04
字體:
來源:轉載
供稿:網友
  • 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($html' target='_blank'>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 = JennerZebraArrayGroupBy::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 JennerZebraArrayGroupBy($records))->groupByField($group_by_fields)->groupByValue($group_by_values)->groupByField($week_fields)->groupByValue($week_values)->get();print_r($grouped);

        舉例

          歸并過程中,實現對結果的中值計算歸并過程中,對時間字段進行自定義處理,例如歸并每5分鐘的數據等等

          PHP編程

          鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 逊克县| 巩留县| 石狮市| 南昌市| 观塘区| 合阳县| 永登县| 邓州市| 溧水县| 阿克陶县| 胶南市| 贵州省| 奎屯市| 临武县| 射阳县| 长寿区| 荥阳市| 孝感市| 颍上县| 肇州县| 鸡泽县| 武宣县| 三门县| 宝丰县| 增城市| 乐至县| 闵行区| 平湖市| 蕉岭县| 精河县| 旬邑县| 昌宁县| 镇雄县| 马公市| 辉南县| 天峨县| 永修县| 千阳县| 铜鼓县| 武功县| 右玉县|