How to group subarrays by a column value?

前端 未结 18 1803
一生所求
一生所求 2020-11-22 10:43

I have the following array

Array
(
    [0] => Array
        (
            [id] => 96
            [shipping_no] => 212755-1
            [part_no] =&         


        
相关标签:
18条回答
  • 2020-11-22 11:06

    Recursive function grouping 2-dimensional array by keys from first to last

    Input:

    $arr = array(
        '0' => array(
            'key0' => 'value0',
            'key1' => 'value1',
            'key2' => 'value02',
        ),
        '2' => array(
            'key0' => 'value0',
            'key1' => 'value1',
            'key2' => 'value12',
        ),
        '3' => array(
            'key0' => 'value0',
            'key1' => 'value3',
            'key2' => 'value22',
        ),
    );
    $keys = array('key0', 'key1', 'key2');
    

    Output:

    $arr = array(
        'value0' => array(
            'value1 => array(
                'value02' => null,
                'value12' => null,
            ),
            'value3' => 'value22',
        ),
    );
    

    Code:

    function array_group_by_keys(&$arr, $keys) {
    
        if (count($arr) < 2){
            $arr = array_shift($arr[0]);
            return;
        }
    
        foreach ($arr as $k => $item) {
            $fvalue = array_shift($item);
            $arr[$fvalue][] = $item;
            unset($arr[$k]);
        }
    
        array_shift($keys);
        foreach ($arr as &$sub_arr) {
            array_group_by_keys($sub_arr, $keys);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:07

    Consume and cache the column value that you want to group by, then push the remaining data as a new subarray of the group you have created in the the result.

    function array_group(array $data, $by_column)
    {
        $result = [];
        foreach ($data as $item) {
            $column = $item[$by_column];
            unset($item[$by_column]);
            $result[$column][] = $item;
        }
        return $result;
    }
    
    0 讨论(0)
  • 2020-11-22 11:10
    function array_group_by($arr, array $keys) {
    
    if (!is_array($arr)) {
        trigger_error('array_group_by(): The first argument should be an array', E_USER_ERROR);
    }
    if (count($keys)==0) {
        trigger_error('array_group_by(): The Second argument Array can not be empty', E_USER_ERROR);
    }
    
    // Load the new array, splitting by the target key
    $grouped = [];
    foreach ($arr as $value) {
        $grouped[$value[$keys[0]]][] = $value;
    }
    
    // Recursively build a nested grouping if more parameters are supplied
    // Each grouped array value is grouped according to the next sequential key
    if (count($keys) > 1) {
            foreach ($grouped as $key => $value) {
           $parms = array_merge([$value], [array_slice($keys, 1,count($keys))]);
           $grouped[$key] = call_user_func_array('array_group_by', $parms);
    
        }
    }
    return $grouped;
    

    }

    0 讨论(0)
  • 2020-11-22 11:11

    There is no native one, just use a loop.

    $result = array();
    foreach ($data as $element) {
        $result[$element['id']][] = $element;
    }
    
    0 讨论(0)
  • 2020-11-22 11:14
    $arr = array();
    
    foreach($old_arr as $key => $item)
    {
       $arr[$item['id']][$key] = $item;
    }
    
    ksort($arr, SORT_NUMERIC);
    
    0 讨论(0)
  • 2020-11-22 11:16

    Check indexed function from Nspl:

    use function \nspl\a\indexed;
    $grouped = indexed($data, 'id');
    
    0 讨论(0)
提交回复
热议问题