如何简化我的PHP数组

时间:2015-06-10 12:04:02

标签: php arrays

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

问题是我不知道像欧洲'欧洲'或者' france',或者' paris'。

我需要确定该大陆是否是新密钥,因此我使用它创建一个数组。 与国家相同,然后是城市等。

我有这个:

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
                )
            )
        )
    )
)

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

使用循环 -

$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"
      }
    }
  }
}

答案 1 :(得分:0)

这个应该有用,它真的很糟糕......

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