Add multidimensional PHP array

0

Good morning, I have the following array:

Array (  [0] => Array(  [data] => 2018-06-08
                        [grupo] => 1 
                        [maq] => 1
                        [segundos] => 10089 ) 
         [1] => Array ( [data] => 2018-06-08 
                        [grupo] => 1 
                        [maq] => 2 
                        [segundos] => 6300 ) 
        [2] => Array  ( [data] => 2018-06-08 
                        [grupo] => 1 
                        [maq] => 1 
                        [segundos] => 3600 ) 
        [3] => Array (  [data] => 2018-06-09
                        [grupo] => 1
                        [maq] => 1 
                        [segundos] => 3600 ) )

My question is as follows, how can I add only the key, where the key [date] and [maq] were the same (repeat), like the following output:

 Array (  [0] => Array( [data] => 2018-06-08
                        [grupo] => 1 
                        [maq] => 1
                        [segundos] => 13689 ) 
         [1] => Array ( [data] => 2018-06-08 
                        [grupo] => 1 
                        [maq] => 2 
                        [segundos] => 6300 ) 
        [2] => Array (  [data] => 2018-06-09
                        [grupo] => 1
                        [maq] => 1 
                        [segundos] => 3600 ) )
    
asked by anonymous 13.06.2018 / 15:56

1 answer

0

I got the following code:

$second_array = array_reduce($first_array, function($carry, $item) {
    $key = sprintf('%s-%s', $item['data'], $item['maq']);
    if (isset($carry[$key])) {
        $carry[$key]['segundos'] += $item['segundos'];
    } else {
        $carry[$key] = $item;
    }
    return $carry;
});


$second_array = array_values($second_array);
    
13.06.2018 / 20:46