3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ 'KeyAccountID' => 1234, 'KeyAccountName' => 'John, Lennon', 'ClientID' => 9999, 'Client' => 'BBC', 'projects' => [ [2,915,'Zyxeldy','','',15000,'','',''], [2,956,'Awesome project, Step 1 ','','',1833.3333,1833.3333,1833.3333,''], [2,957,'Awesome project, Step 2','','',7000,'','',''] ] ],[ 'KeyAccountID' => 1236, 'KeyAccountName' => 'Ringo Starr', 'ClientID' => 9997, 'Client' => 'XYY', 'projects' => [ [2,867,'Data Mining','','',10000,'','',''] ] ],[ 'KeyAccountID' => 1235, 'KeyAccountName' => 'Poul McCartney', 'ClientID' => 9996, 'Client' => 'XYZ', 'projects' => [ [2,715,'XYZ, CSM','','',22083.3333,22083.3333,22083.3333,''] ] ] ]; $newArray = []; foreach($array as $value) { $newArray[$value['KeyAccountID']]['month1'] = array_sum(array_column($value['projects'],5)); $newArray[$value['KeyAccountID']]['month2'] = array_sum(array_column($value['projects'],6)); $newArray[$value['KeyAccountID']]['month3'] = array_sum(array_column($value['projects'],7)); } echo "<pre>"; var_dump($newArray); ?>
Output for 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Warning: array_sum(): Addition is not supported on type string in /in/UEFaB on line 35 Warning: array_sum(): Addition is not supported on type string in /in/UEFaB on line 35 Warning: array_sum(): Addition is not supported on type string in /in/UEFaB on line 36 Warning: array_sum(): Addition is not supported on type string in /in/UEFaB on line 36 Warning: array_sum(): Addition is not supported on type string in /in/UEFaB on line 35 Warning: array_sum(): Addition is not supported on type string in /in/UEFaB on line 36 <pre>array(3) { [1234]=> array(3) { ["month1"]=> float(23833.3333) ["month2"]=> float(1833.3333) ["month3"]=> float(1833.3333) } [1236]=> array(3) { ["month1"]=> int(10000) ["month2"]=> int(0) ["month3"]=> int(0) } [1235]=> array(3) { ["month1"]=> float(22083.3333) ["month2"]=> float(22083.3333) ["month3"]=> float(22083.3333) } }
Output for 7.1.25 - 7.1.33, 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
<pre>array(3) { [1234]=> array(3) { ["month1"]=> float(23833.3333) ["month2"]=> float(1833.3333) ["month3"]=> float(1833.3333) } [1236]=> array(3) { ["month1"]=> int(10000) ["month2"]=> int(0) ["month3"]=> int(0) } [1235]=> array(3) { ["month1"]=> float(22083.3333) ["month2"]=> float(22083.3333) ["month3"]=> float(22083.3333) } }

preferences:
136.76 ms | 410 KiB | 5 Q