php 将具有3个元素行的数组转换为具有3个级别的分层数组

hujrc8aj  于 2023-04-04  发布在  PHP
关注(0)|答案(4)|浏览(179)

我有一个数组,看起来像这样:

[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

确切的预期结果:

array (
  'Latin America' => 
  array (
    'Venezuela' => 
    array (
      0 => 'Caracas',
    ),
  ),
  'Middle East' => 
  array (
    'Egypt' => 
    array (
      0 => 'Cairo',
    ),
    'United Arab Emirates' => 
    array (
      0 => 'Abu Dhabi',
      1 => 'Dubai',
    ),
  ),
  'North America' => 
  array (
    'Canada' => 
    array (
      0 => 'Montreal',
    ),
  ),
)
6xfqseft

6xfqseft1#

$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);
wgeznvg7

wgeznvg72#

还有几种替代方法:
在无主体的foreach()中使用“数组解构”来填充分层结构(Demo)(Further about the technique

$result = [];
foreach (
    $array as
    [
        'Continent' => $a,
        'Country' => $b,
        'City' => $result[$a][$b][]
    ]
);
var_export($result);

使用array_reduce()避免在全局范围内声明结果变量。(Demo

var_export(
    array_reduce(
        $array,
        function($result, $row) {
            $result[$row['Continent']][$row['Country']][] = $row['City'];
            return $result;
        }
    )
);
8fsztsew

8fsztsew3#

那么这个解决方案呢:

function arrayToMultiDimensionalArray(array $elements, array $dimensions)
{
    $output = [];

    foreach ($elements as $element) {
        $outputElement =& $output;

        // Foreach to build up the dimensions
        foreach ($dimensions as $dimension) {
            $outputElement =& $outputElement[$element[$dimension]];
        }

        $outputElement[] = $element['City'];
    }

    return $output;
}

$dimensions = ['Continent', 'Country'];

$tree = arrayToMultiDimensionalArray($array, $dimensions);
lrpiutwd

lrpiutwd4#

我认为你只需要在你的数组中循环,然后创建一个新的数组,使用一些值作为键,另一些值作为值,例如

$new_array = array();
foreach($array as $val)
{ 
     $new_array[$val['Continent']][$val['Country']] = array('City' => $val['City']);
}

Live Sample

相关问题