@Triglav77

Как просуммировать все данные в массиве по одному из полей?

Есть массив $arr

Array ( 
	[0] => Array ( [times] => 13.02.2020 [quantity] => 0 ) 
	[1] => Array ( [times] => 12.02.2020 [quantity] => 0 ) 
	[2] => Array ( [times] => 11.02.2020 [quantity] => 0 ) 
	[3] => Array ( [times] => 10.02.2020 [quantity] => 0 ) 
	[4] => Array ( [times] => 09.02.2020 [quantity] => 0 ) 
	[5] => Array ( [times] => 08.02.2020 [quantity] => 0 ) 
	[6] => Array ( [times] => 07.02.2020 [quantity] => 0 ) 
	[7] => Array ( [times] => 06.02.2020 [quantity] => 0 ) 
	[8] => Array ( [times] => 05.02.2020 [quantity] => 0 ) 
	[9] => Array ( [times] => 04.02.2020 [quantity] => 0 ) 
	[10] => Array ( [times] => 03.02.2020 [quantity] => 0 ) 
	[11] => Array ( [times] => 02.02.2020 [quantity] => 0 ) 
	[12] => Array ( [times] => 01.02.2020 [quantity] => 0 ) 
	[13] => Array ( [times] => 31.01.2020 [quantity] => 0 ) 
	[14] => Array ( [times] => 30.01.2020 [quantity] => 0 ) 
	[15] => Array ( [times] => 29.01.2020 [quantity] => 0 ) 
	[16] => Array ( [times] => 28.01.2020 [quantity] => 0 ) 
	[17] => Array ( [times] => 27.01.2020 [quantity] => 0 ) 
	[18] => Array ( [times] => 26.01.2020 [quantity] => 0 ) 
	[19] => Array ( [times] => 25.01.2020 [quantity] => 0 ) 
	[20] => Array ( [times] => 24.01.2020 [quantity] => 0 ) 
	[21] => Array ( [times] => 23.01.2020 [quantity] => 0 ) 
	[22] => Array ( [times] => 22.01.2020 [quantity] => 0 ) 
	[23] => Array ( [times] => 21.01.2020 [quantity] => 0 ) 
	[24] => Array ( [times] => 20.01.2020 [quantity] => 0 ) 
	[25] => Array ( [times] => 19.01.2020 [quantity] => 0 ) 
	[26] => Array ( [times] => 18.01.2020 [quantity] => 0 ) 
	[27] => Array ( [times] => 17.01.2020 [quantity] => 0 ) 
	[28] => Array ( [times] => 16.01.2020 [quantity] => 0 ) 
	[29] => Array ( [times] => 15.01.2020 [quantity] => 0 ) 
	[30] => Array ( [times] => 14.01.2020 [quantity] => 0 ) 
	[31] => Array ( [times] => 01.02.2020 [quantity] => 5 ) 
	[32] => Array ( [times] => 13.01.2020 [quantity] => 10 ) 
	[33] => Array ( [times] => 14.01.2020 [quantity] => 4 ) 
	[34] => Array ( [times] => 15.01.2020 [quantity] => 2 ) 
	[35] => Array ( [times] => 16.01.2020 [quantity] => 6 ) 
	[36] => Array ( [times] => 17.01.2020 [quantity] => 2 ) 
	[37] => Array ( [times] => 19.01.2020 [quantity] => 9 ) 
	[38] => Array ( [times] => 20.01.2020 [quantity] => 2 ) 
	[39] => Array ( [times] => 22.01.2020 [quantity] => 10 ) 
	[40] => Array ( [times] => 24.01.2020 [quantity] => 17 ) 
	[41] => Array ( [times] => 25.01.2020 [quantity] => 1 ) 
	[42] => Array ( [times] => 27.01.2020 [quantity] => 6 ) 
	[43] => Array ( [times] => 29.01.2020 [quantity] => 11 ) 
	[44] => Array ( [times] => 30.01.2020 [quantity] => 3 ) 
)


Как просуммировать все quantity для одинаковых times

чтобы получилось один уникальный times и сумма quantity
  • Вопрос задан
  • 90 просмотров
Решения вопроса 1
@Apostol63
$arr2 = [];
        foreach($arr as $item) {
            if(!empty($arr2[$item['times']])) {
                $arr2[$item['times']] = $arr2[$item['times']] + (int) $item['quantity'];
            } else {
                $arr2[$item['times']] = (int) $item['quantity'];
            }

        }


$arr - ваш исходный массив
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
0xD34F
@0xD34F
$grouped = [];

foreach ($arr as [ 'times' => $t, 'quantity' => $q ]) {
  $grouped[$t] = ($grouped[$t] ?? 0) + $q;
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы