gpt4 book ai didi

php - 如何在 PHP 中将单个数组转换为多维数组?

转载 作者:行者123 更新时间:2023-12-03 20:21:59 24 4
gpt4 key购买 nike

所以我有一个看起来像这样的数组:

[98] => Array
(
[City] => Caracas
[Country] => Venezuela
[Continent] => Latin America
)

[99] => Array
(
[City] => Cairo
[Country] => Egypt
[Continent] => Middle East
)

[105] => Array
(
[City] => Abu Dhabi
[Country] => United Arab Emirates
[Continent] => Middle East
)

[106] => Array
(
[City] => Dubai
[Country] => United Arab Emirates
[Continent] => Middle East
)

[107] => Array
(
[City] => Montreal
[Country] => Canada
[Continent] => North America
)

我正在尝试将此数组排序为多维数组,以便它输出如下内容:
Continent
- Country Name
-- Cities under Every Country

我正在使用 PHP。任何人都可以指出我应该如何做这件事的一个很好的起点吗?

最佳答案

$array = array(
98 => array(
'City' => 'Caracas',
'Country' => 'Venezuela',
'Continent' => 'Latin America',
),
99 => array(
'City' => 'Cairo',
'Country' => 'Egypt',
'Continent' => 'Middle East',
),
105 => array(
'City' => 'Abu Dhabi',
'Country' => 'United Arab Emirates',
'Continent' => 'Middle East',
),
106 => array(
'City' => 'Dubai',
'Country' => 'United Arab Emirates',
'Continent' => 'Middle East',
),
107 => array(
'City' => 'Montreal',
'Country' => 'Canada',
'Continent' => 'North America',
)
);

$newArray = array();
foreach ($array as $row)
{
$newArray[$row['Continent']][$row['Country']][] = $row['City'];
}

print_r($newArray);

关于php - 如何在 PHP 中将单个数组转换为多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19911822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com