3v4l.org

run code in 300+ PHP versions simultaneously
<?php $locations = Array( Array ( 'id' => 14, 'post_id' => 319, 'location' => 'Atlanta, GA', 'address' => '161 Racetrack Rd., McDonough', 'city' => 'Atlanta', 'state' => 'Georgia', 'state_code' =>'GA', 'country' => 'USA', 'zipcode' => 30253, 'service_zipcode' => '', 'phone' => '', 'fax' => '', 'email' => '', 'facebook' => '', 'twitter' => '', 'linkedin' => '', 'gplus' => '', 'latitude' => '', 'longitude' => '', 'url' => '', 'created_at' => '2019-04-22 07:22:55', 'updated_at' => '2019-04-22 07:26:31', ), Array ( 'id' => 16, 'post_id' => 321, 'location' => 'Augusta, Georigia', 'address' => '1446 Harper Street', 'city' => 'Augusta', 'state' => 'Georigia', 'state_code' => 'GA', 'country' => 'USA', 'zipcode' => '', 'service_zipcode' => '', 'phone' => '', 'fax' => '', 'email' => '', 'facebook' => '', 'twitter' => '', 'linkedin' => '', 'gplus' => '', 'latitude' => '', 'longitude' => '', 'url' => '', 'created_at' => '2019-04-22 07:29:33', 'updated_at' => '2019-04-22 07:32:25', ), Array ( 'id' => 12, 'post_id' => 317, 'location' => 'Savannah, Georgia', 'address' => '834 Northside Dr. East, Statesboro', 'city' => 'Savannah', 'state' => 'Georgia', 'state_code' => 'GA', 'country' => 'USA', 'zipcode' => 30458, 'service_zipcode' => '', 'phone' => '', 'fax' => '', 'email' => '', 'facebook' => '', 'twitter' => '', 'linkedin' => '', 'gplus' => '', 'latitude' => '', 'longitude' => '', 'url' => '', 'created_at' => '2019-04-22 07:15:43', 'updated_at' => '2019-04-22 07:18:11', ) ); $hierarchy = []; if (! empty($locations)) { foreach ($locations as $location) { $hierarchy[$location['country']][$location['state']] = $location; } } if (! empty($hierarchy)) { foreach ($hierarchy as $country => $location) { echo "<br/>" . $country; if (! empty($hierarchy[$country])) { foreach ($hierarchy[$country] as $state => $city) { echo "<br/>--> " . $state; echo "<br/>----> " . $city['city']; } } } }
Output for 7.1.0 - 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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
<br/>USA<br/>--> Georgia<br/>----> Savannah<br/>--> Georigia<br/>----> Augusta

preferences:
139.5 ms | 403 KiB | 212 Q