PHP code example of micheledurante / array-aggregate

1. Go to this page and download the library: Download micheledurante/array-aggregate 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/ */

    

micheledurante / array-aggregate example snippets


function array_aggregate(array $columns, array $rows, callable $compute_func = null): array

$rows = [
    0 => [
        'store_id' => 1,
        'manager_id' => 2,
        'name' => 'Alice'
    ],
    1 => [
        'store_id' => 2,
        'manager_id' => 3,
        'name' => 'Bob'
    ],
    2 => [
        'store_id' => 2,
        'manager_id' => 3,
        'name' => 'Eve'
    ],
    3 => [
        'store_id' => 2,
        'manager_id' => 4,
        'name' => 'Foobar'
    ]
];

$groups = array_aggregate(array('store_id', 'manager_id'), $rows, function (array $group): array {
    return [
        'store_id' => $group[0]['store_id'],
        'manager_id' => $group[0]['manager_id'],
        'people' => implode(',', array_column($group, 'name'))
    ];
});

var_export($groups);

/*
array (
    0 => array (
        'store_id' => 1,
        'manager_id' => 2,
        'people' => 'Alice'
    ),
    1 => array (
        'store_id' => 2,
        'manager_id' => 3,
        'people' => 'Bob,Eve'
    ),
    2 => array (
        'store_id' => 2,
        'manager_id' => 4,
        'people' => 'Foobar'
    )
)
*/