3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $arrayA = array( 0 => array( 'id' => 4701, 'start' => '2018-04-15 17:00:00', 'start2' => '2018-04-15 19:00:00', 'expiry' => '2018-04-16 01:00:00', 'expiry2' => '2018-05-16 03:00:00', 'threat' => 'Rain', 'level' => 2, 'last_updated' => '2018-05-09 14:11:54', 'created' => '2018-04-12 15:05:50', 'county' => 'Luzerne', 'state' => 'Pennsylvania', 'image' => '15238041824facec0b183878da58e007b4b6.png', ), 1 => array( 'id' => 4947, 'start' => '2018-04-26 11:11:00', 'start2' => '2018-04-26 15:10:00', 'expiry' => '2018-04-30 23:00:00', 'expiry2' => '2018-05-30 08:00:00', 'threat' => 'Thunderstorm', 'level' => 3, 'last_updated' => '2018-05-09 12:33:25', 'created' => '2018-04-26 15:11:45', 'county' => 'Yellowstone', 'state' => 'Montana', 'image' => '1524755505c1beae47ebbb285beaa6a1c672.png', ), 2 => array( 'id' => 4947, 'start' => '2018-04-26 11:11:00', 'start2' => '2018-04-26 15:10:00', 'expiry' => '2018-04-30 23:00:00', 'expiry2' => '2018-05-30 08:00:00', 'threat' => 'Thunderstorm', 'level' => 3, 'last_updated' => '2018-05-09 12:33:25', 'created' => '2018-04-26 15:11:45', 'county' => 'Luzerne', 'state' => 'Pennsylvania', 'image' => '1524755505c1beae47ebbb285beaa6a1c672.png', ), ); $arrayB = array( 0 => array( 'id' => 4701, 'start' => '2018-04-15 17:00:00', 'start2' => '2018-04-15 19:00:00', 'expiry' => '2018-04-16 01:00:00', 'expiry2' => '2018-05-16 03:00:00', 'threat' => 'Rain', 'level' => 2, 'last_updated' => '2018-05-09 14:11:54', 'created' => '2018-04-12 15:05:50', 'county' => 'Luzerne', 'state' => 'Pennsylvania', 'image' => '15238041824facec0b183878da58e007b4b6.png', 'affected_locations' => array( 0 => array( 'id' => 4701, 'start' => '2018-04-15 17:00:00', 'start2' => '2018-04-15 19:00:00', 'expiry' => '2018-04-16 01:00:00', 'expiry2' => '2018-05-16 03:00:00', 'threat' => 'Rain', 'level' => 2, 'last_updated' => '2018-05-09 14:11:54', 'created' => '2018-04-12 15:05:50', 'county' => 'Luzerne', 'state' => 'Pennsylvania', 'image' => '15238041824facec0b183878da58e007b4b6.png', ), ), ), 1 => array( 'id' => 4947, 'start' => '2018-04-26 11:11:00', 'start2' => '2018-04-26 15:10:00', 'expiry' => '2018-04-30 23:00:00', 'expiry2' => '2018-05-30 08:00:00', 'threat' => 'Thunderstorm', 'level' => 3, 'last_updated' => '2018-05-09 12:33:25', 'created' => '2018-04-26 15:11:45', 'county' => 'Yellowstone', 'state' => 'Montana', 'image' => '1524755505c1beae47ebbb285beaa6a1c672.png', 'affected_locations' => array( 0 => array( 'id' => 4947, 'start' => '2018-04-26 11:11:00', 'start2' => '2018-04-26 15:10:00', 'expiry' => '2018-04-30 23:00:00', 'expiry2' => '2018-05-30 08:00:00', 'threat' => 'Thunderstorm', 'level' => 3, 'last_updated' => '2018-05-09 12:33:25', 'created' => '2018-04-26 15:11:45', 'county' => 'Yellowstone', 'state' => 'Montana', 'image' => '1524755505c1beae47ebbb285beaa6a1c672.png', ), 1 => array( 'id' => 4947, 'start' => '2018-04-26 11:11:00', 'start2' => '2018-04-26 15:10:00', 'expiry' => '2018-04-30 23:00:00', 'expiry2' => '2018-05-30 08:00:00', 'threat' => 'Thunderstorm', 'level' => 3, 'last_updated' => '2018-05-09 12:33:25', 'created' => '2018-04-26 15:11:45', 'county' => 'Luzerne', 'state' => 'Pennsylvania', 'image' => '1524755505c1beae47ebbb285beaa6a1c672.png', ), ), ), ); #print_r($arrayA); #echo "\n\r"; #echo "Looking to turn the above array into the array below:\n\r"; #print_r($arrayB); foreach ($arrayA as $element) { $arrayC[$element["id"]][] = $element; } print_r($arrayC);
based on XEu2A
Output for 5.6.0 - 5.6.30, hhvm-3.22.0, 7.1.0 - 7.3.1
Array ( [4701] => Array ( [0] => Array ( [id] => 4701 [start] => 2018-04-15 17:00:00 [start2] => 2018-04-15 19:00:00 [expiry] => 2018-04-16 01:00:00 [expiry2] => 2018-05-16 03:00:00 [threat] => Rain [level] => 2 [last_updated] => 2018-05-09 14:11:54 [created] => 2018-04-12 15:05:50 [county] => Luzerne [state] => Pennsylvania [image] => 15238041824facec0b183878da58e007b4b6.png ) ) [4947] => Array ( [0] => Array ( [id] => 4947 [start] => 2018-04-26 11:11:00 [start2] => 2018-04-26 15:10:00 [expiry] => 2018-04-30 23:00:00 [expiry2] => 2018-05-30 08:00:00 [threat] => Thunderstorm [level] => 3 [last_updated] => 2018-05-09 12:33:25 [created] => 2018-04-26 15:11:45 [county] => Yellowstone [state] => Montana [image] => 1524755505c1beae47ebbb285beaa6a1c672.png ) [1] => Array ( [id] => 4947 [start] => 2018-04-26 11:11:00 [start2] => 2018-04-26 15:10:00 [expiry] => 2018-04-30 23:00:00 [expiry2] => 2018-05-30 08:00:00 [threat] => Thunderstorm [level] => 3 [last_updated] => 2018-05-09 12:33:25 [created] => 2018-04-26 15:11:45 [county] => Luzerne [state] => Pennsylvania [image] => 1524755505c1beae47ebbb285beaa6a1c672.png ) ) )