3v4l.org

run code in 300+ PHP versions simultaneously
<?php $qp = [ ['Nsd_id' => "1@2@3@4@5", 'Nsd_amount' => "0@0@0@20000@0"], ['Nsd_id' => "1@2@3@4@5", 'Nsd_amount' => "0@0@12000@0@0"], ['Nsd_id' => "1@2@3@4@5", 'Nsd_amount' => "0@0@0@10000@0"], ['Nsd_id' => "1@2@3@4@5", 'Nsd_amount' => "0@0@10000@0@0"], ['Nsd_id' => "1@2@3@4@5@12", 'Nsd_amount' => "0@0@10000@0@0@1000"], ['Nsd_id' => "1@2@3@4@5@12@14@15", 'Nsd_amount' => "0@0@8000@0@0@0@0@0"], ['Nsd_id' => "1@2@3@4@5@12@14@15", 'Nsd_amount' => "0@0@0@0@0@0@13000@0"], ['Nsd_id' => "1@2@3@4@5@12@14@15", 'Nsd_amount' => "0@2500@0@3900@0@0@20000@0"], ['Nsd_id' => "1@2@3@4@5@12@14@15", 'Nsd_amount' => "0@0@5000@0@0@0@0@0"], ['Nsd_id' => "1@2@3@4@5@12@14@15", 'Nsd_amount' => "0@0@5000@0@0@1000@0@0"], ['Nsd_id' => "1@2@3@4@5@12@14@15", 'Nsd_amount' => "0@0@2000@0@0@500@0@0"], ['Nsd_id' => "1@2@3@4@5@12@14@15", 'Nsd_amount' => "0@0@0@0@0@0@4000@0"], ]; $nsdIds = []; $result = []; foreach ($qp as $key => $value) { // exploding Nsd_ids $nsdTemp = explode("@", $value['Nsd_id']); // exploding nsd_amounts $nsdAmount = explode("@", $value['Nsd_amount']); // saving all new nsd ids and only keep unique nsd ids $nsdIds = array_unique(array_merge($nsdIds, $nsdTemp)); // I am now combining keeping nsd id as key and nsd amount as its value $temp1 = array_combine($nsdTemp, $nsdAmount); foreach ($nsdIds as $value1) { // for latest php version // $temp1 contains keys of nsd ids // value1 contains nsdids we are sure that it will be in nsdids as we are already merging those ids // then fetching amounf for that nsdids $result[$value1] = ($result[$value1] ?? 0) + $temp1[$value1]; // for php version less than 7 // $result[$value1] = (!empty($result[$value1]) ? $result[$value1] : 0) + $result[$temp1[$value1]]; } } print_r($result);die;
Output for 7.1.25 - 7.1.30, 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
Array ( [1] => 0 [2] => 2500 [3] => 52000 [4] => 33900 [5] => 0 [12] => 2500 [14] => 37000 [15] => 0 )

preferences:
141.54 ms | 408 KiB | 5 Q