{
"issuance": {
"1": {
"data": "01.20",
"quantity": 2
},
"2": {
"data": "01.20",
"quantity": 4
},
"3": {
"data": "04.20",
"quantity": 2
},
"4": {
"data": "04.20",
"quantity": 2
}
}
{
"issuance": {
"1": {
"data": "01.20",
"quantity": 6
},
"2": {
"data": "04.20",
"quantity": 4
}
}
<?php
$json = <<<JSON
{
"issuance": {
"1": {
"data": "01.20",
"quantity": 2
},
"2": {
"data": "01.20",
"quantity": 4
},
"3": {
"data": "04.20",
"quantity": 2
},
"4": {
"data": "04.20",
"quantity": 2
}
}
}
JSON
;
$arr = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$res = new \stdClass();
$res->issuance = array_values(
array_reduce(
$arr['issuance'],
function ($res, $el) {
$res[$el['data']]['data'] = $el['data'];
$res[$el['data']]['quantity'] = isset($res[$el['data']]['quantity'])
? $res[$el['data']]['quantity'] + $el['quantity']
: $el['quantity'];
return $res;
},
[]
)
);
var_dump(json_encode($res, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT, 512));