print_r($stocks);
[597] => Array
.......
[598] => Array
(
[0] => Array
(
[UF_QUANTITY] => 2
[UF_BRAK] => 2
[UF_WAREHOUSENAME] => Химки
[UF_SUBJECT] => Лента праздничная
)
[1] => Array
(
[UF_QUANTITY] => 8
[UF_BRAK] => 3
[UF_WAREHOUSENAME] => Мытищи
[UF_SUBJECT] => Лента праздничная
)
)
[599] => Array
.......
[597] => Array
.......
[598] => Array
(
[UF_QUANTITY] => 10
[UF_BRAK] => 5
[UF_WAREHOUSENAME] => Химки(2 шт), Мытищи (8 шт)
[UF_SUBJECT] => Лента праздничная
)
[599] => Array
.......
$grouped = [];
foreach ($arr as $key => $items) {
$group = [
'UF_SUBJECT' => $items[0]['UF_SUBJECT'],
'UF_QUANTITY' => 0,
'UF_BRAK' => 0,
'UF_WAREHOUSENAME' => [],
];
foreach ($items as $item) {
$group['UF_QUANTITY'] += $item['UF_QUANTITY'];
$group['UF_BRAK'] += $item['UF_BRAK'];
$group['UF_WAREHOUSENAME'][] = $item['UF_WAREHOUSENAME']." (".$item['UF_QUANTITY']." шт)";
}
$group['UF_WAREHOUSENAME'] = implode(', ', $group['UF_WAREHOUSENAME']);
$grouped[$key] = $group;
}