【问题标题】:How can I simplify my PHP array如何简化我的 PHP 数组
【发布时间】:2015-08-25 16:11:04
【问题描述】:

我有一个简单的数据数组,我想让它成为多维的。 为了简单起见,我在下面写了一个反映我所拥有的数组和另一个反映它应该如何的数组。

问题是我不知道“欧洲”或“法国”或“巴黎”之类的键。

我需要确定大陆是否是一个新键,所以我用它做一个数组。 与国家相同,然后是城市等。

我有这个:

Array
(
    [0] => Array
        (
            [continent]     => europe
            [country]       => france
            [city]          => paris
            [monument]      => tour eiffel
        )

    [1] => Array
        (
            [continent]     => europe
            [country]       => england
            [city]          => london
            [monument]      => big ben
        )

    [2] => Array
        (
            [continent]     => australia
            [country]       => australia
            [city]          => sydney
            [monument]      => opera
        )

    [3] => Array
        (
            [continent]     => america
            [country]       => usa
            [city]          => new york
            [monument]      => empire state building 
        )

    [4] => Array
        (
            [continent]     => america
            [country]       => usa
            [city]          => new york
            [monument]      => statue of liberty 
        )
)

我需要那个:

Array
(
    [0] => Array (
        [europe] => Array (
            [france] => Array (
                [paris] => Array (
                    [0] => eiffel tower
                )
            )
            [england] => Array (
                [london] => Array (
                    [0] => big ben
                )
            )
        )   
        [australia] => Array (
            [australia] => Array (
                [sydney] => Array (
                    [0] => opera
                )
            )
        )
        [america] => Array (
            [usa] => Array (
                [new york] => Array (
                    [0] => empire state building
                    [1] => statue of liberty
                )
            )
        )
    )
)

有什么想法吗?

【问题讨论】:

  • 只需遍历它们并生成数组。
  • 我不认为“简化”意味着你认为它的意思......
  • @b0s3 是的,这正是我正在寻求帮助的原因......

标签: php arrays


【解决方案1】:

使用循环 -

$new = array();

foreach($array as $data) {
    $new['continents'][$data['continent']][$data['country']][$data['city']][] = $data['monument']; 
}

输出

array(1) {
  ["continents"]=>
  array(3) {
    ["europe"]=>
    array(2) {
      ["france"]=>
      array(1) {
        ["paris"]=>
        string(11) "tour eiffel"
      }
      ["england"]=>
      array(1) {
        ["london"]=>
        string(7) "big ben"
      }
    }
    ["australia"]=>
    array(1) {
      ["australia"]=>
      array(1) {
        ["sydney"]=>
        string(5) "opera"
      }
    }
    ["america"]=>
    array(1) {
      ["usa"]=>
      array(1) {
        ["new york"]=>
        string(17) "statue of liberty"
      }
    }
  }
}

【讨论】:

  • 我不能使用它,因为我不知道密钥。我已经编辑了我的问题。
  • 不会影响。您不知道值,但键(continentcountrycitymonument)将被修复。也为他们工作。
【解决方案2】:

这个应该能用,真的不好读……

$target = array();
foreach($source as $item) {
    //add continent
    if (!array_key_exists($item['continent'], $target) 
        $target[$item['continent']] = array();
    //add country
    if (!array_key_exists($item['country'], $target[$item['continent']]) 
        $target[$item['continent']][$item['country']] = array();
    //add city
    if (!array_key_exists($item['city'], $target[$item['continent']][$item['country']]) 
        $target[$item['continent']][$item['country']][$item['city']] = array();
    //add the monument string to the city array
    $target[$item['continent']][$item['country']][$item['city']][] = $item['monument'];
}

关于创建子数组或更简单的数组构建有什么建议吗?

你可以像这样读取你的新数组:

foreach ($target as $continent => $con_arr)
    foreach ($con_arr as $country => $cou_arr)
        foreach ($cou_arr as $city=> $cit_arr)
            foreach ($cou_arr as $monument)
                print $continent . $country . $city . $monument;

【讨论】:

  • 我不能使用它,因为我不知道密钥。我已经编辑了我的问题。
  • 'europe' 或 'france' 或 'paris' 不是键,它们是值。这将根据您的需要进行构建。如果您在读取新数组时遇到问题,我会理解您的评论,请在您的上一个问题中再解释一下。
猜你喜欢
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
相关资源
最近更新 更多