Шаги для реализации:
1) Пожалуйста, переберите текущий массив.
2) Создайте новый пустой массив.
3) Добавьте к новому массиву с помощью country
в качестве ключа,
4) Добавить к новому массиву с state
в качестве ключа,
Теперь зациклите новый массив и напечатайте страну и состояние в отдельной строке.
<?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'];
}
}
}
}
Выход:
USA
--> Georgia
----> Savannah
--> Georigia
----> Augusta
Where `USA` -> Country Name
Starting with `-->`: state name
Starting with `---->`: city name
Демонстрационная версия: