PHP類比SQL的GROUP BY演算法

來源:互聯網
上載者:User
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){            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);

    舉例

  • 歸併過程中,實現對結果的中值計算
  • 歸併過程中,對時間欄位進行自訂處理,例如歸併每5分鐘的資料
  • 等等
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.