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);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
[{"jumlah":"30","thn":"2017"},{"jumlah":562,"thn":"2018"},{"jumlah":32,"thn":"2019"}]
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 [{"jumlah":"30","thn":"2017"},{"jumlah":562,"thn":"2018"},{"jumlah":32,"thn":"2019"}]

preferences:
154.53 ms | 402 KiB | 178 Q