Group 2d array data by one column and sum other columns in each group (separately)

You’d have to do this manually using a loop. Something like this should work:

$result = array();

foreach( $input as $row) {
    $id = array_shift( $row);

    foreach( $row as $key => $value) {

        $result[ $id ][ $key ] = 
            ( isset( $result[ $id ][ $key ]) ? 
                  $result[ $id ][ $key ] + $value : 
                  $value
            );
    }
}

Output:

array(2) {
  [111]=>
  array(2) {
    [0]=>
    int(10)
    [1]=>
    int(11)
  }
  [222]=>
  array(2) {
    [0]=>
    int(5)
    [1]=>
    int(3)
  }
}

Demo

Leave a Comment