PHP code example of jenner / array_group_by

1. Go to this page and download the library: Download jenner/array_group_by library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jenner / array_group_by example snippets


$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($data){
            return array_sum(array_column($data, 'price'));
        },
];

$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-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-04 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,],
    ['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($data){
            return date('w', strtotime($data[0]['bill_time']));
        },
    'price' => function($data){
            return array_sum(array_column($data, 'price'));
        },
    'cnt' => function($data){
            return array_sum(array_column($data, 'cnt'));
        }
];

$grouped = (new \Jenner\Zebra\ArrayGroupBy($records))
            ->groupByField($group_by_fields)->groupByValue($group_by_values)
            ->groupByField($week_fields)->groupByValue($week_values)->get();
print_r($grouped);

// order by 示例
$order_result = (new \Jenner\Zebra\ArrayGroupBy($records))
            ->groupByField($group_by_fields)
            ->groupByValue($group_by_values)->orderBy('bill_time', SORT_DESC, 'price', SORT_ASC);