3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 276, 'CC_qty' => 250, 'Location' => 'NOP11'], ['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 310, 'CC_qty' => 1385, 'Location' => 'NOP01'], ['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 84, 'CC_qty' => 116, 'Location' => 'NOP06'], ['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 432, 'Location' => 'NOP08'], ['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 228, 'CC_qty' => 101, 'Location' => 'NOP04'], ['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 436, 'Location' => 'NOP08'], ['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 62, 'Location' => 'NOP08'], ['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 75, 'CC_qty' => 89, 'Location' => 'NOP05'], ['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 202, 'CC_qty' => 372, 'Location' => 'NOP07'], ]; $result = []; foreach ($array as $row) { if (!isset($result[$row['id']])) { $result[$row['id']] = $row; } else { $result[$row['id']]['qty'] += $row['qty']; // SUM $result[$row['id']]['locationID'] .= ", " . $row['locationID']; // CONCAT $result[$row['id']]['CC_qty'] += $row['CC_qty']; // SUM $result[$row['id']]['Location'] .= ", " . $row['Location']; // CONCAT } } var_export(array_values($result));
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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
array ( 0 => array ( 'id' => 'abc-abc-12', 'qty' => 0, 'locationID' => '276, 310', 'CC_qty' => 1635, 'Location' => 'NOP11, NOP01', ), 1 => array ( 'id' => 'abc-abc-23', 'qty' => 0, 'locationID' => '84, 254, 228', 'CC_qty' => 649, 'Location' => 'NOP06, NOP08, NOP04', ), 2 => array ( 'id' => 'abc-abc-34', 'qty' => 0, 'locationID' => '254, 254', 'CC_qty' => 498, 'Location' => 'NOP08, NOP08', ), 3 => array ( 'id' => 'abc-abc-45', 'qty' => 0, 'locationID' => '75, 202', 'CC_qty' => 461, 'Location' => 'NOP05, NOP07', ), )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
174.5 ms | 408 KiB | 5 Q