3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[{ "thn": "2017", "jumlah": "30" }, { "thn": "2018", "jumlah": "80" }, { "thn": "2018", "jumlah": "64" }, { "thn": "2018", "jumlah": "5" }, { "thn": "2018", "jumlah": "1" }, { "thn": "2018", "jumlah": "1" }, { "thn": "2018", "jumlah": "4" }, { "thn": "2018", "jumlah": "5" }, { "thn": "2018", "jumlah": "198" }, { "thn": "2018", "jumlah": "2" }, { "thn": "2018", "jumlah": "202" }, { "thn": "2019", "jumlah": "31" }, { "thn": "2019", "jumlah": "1" }]'; $array = json_decode($json,true); $finalArray = array(); foreach($array as $arr){ $finalArray[$arr['thn']]['jumlah'] = (isset($finalArray[$arr['thn']]['jumlah']) ? $finalArray[$arr['thn']]['jumlah'] + $arr['jumlah']: $arr['jumlah']); $finalArray[$arr['thn']]['thn'] = $arr['thn']; } $finalArray = array_values($finalArray); echo json_encode($finalArray);

preferences:
53.63 ms | 402 KiB | 5 Q