0

这是我打印其内容时的示例输出:

Array
(
[0] => Array
    (
        [CountryA] => Array
            (
                [ProvinceA] => Array
                    (
                        [CityA] => Array
                            (
                                [SuburbA] => 
                            )
                    )
            )
    )

[1] => Array
    (
        [CountryA] => Array
            (
                [ProvinceA] => Array
                    (
                        [CityA] => Array
                            (
                                [SuburbB] => 
                            )
                    )
            )
    )

[2] => Array
    (
        [CountryA] => Array
            (
                [ProvinceB] => Array
                    (
                        [CityB] => Array
                            (
                                [SuburbC] => 
                            )
                    )
            )
    )

[3] => Array
    (
        [CountryB] => Array
            (
                [ProvinceD] => Array
                    (
                        [CityE] => Array
                            (
                                [SuburbE] => 
                            )
                    )
            )
    )

我想做的是创建一个以某种方式解析它的函数(并且可能创建一个新数组),以便结果看起来像:

Array
(
[0] => Array
    (
        [CountryA] => Array
            (
                [ProvinceA] => Array
                    (
                        [CityA] => Array
                            (
                                [SuburbA] => 
                                [SuburbB} =>
                            )                       
                    )
                [ProvinceB] =>
                  (
                        [CityB] => Array
                            (
                                [SuburbC] => 
                            )                       
                    )
            )
    )


[1] => Array
    (
        [CountryB] => Array
            (
                [ProvinceD] => Array
                    (
                        [CityE] => Array
                            (
                                [SuburbE] => 
                            )
                    )
            )
    )

提前致谢!!

4

1 回答 1

1

改变你的结构,你的数组不应该是这样的:

 Array(
   [0] => Array([Country A] => data),
   [1] => Array([Country B] => data)
 )

但更像这样:

 Array(
   [Country A] => data,
   [Country B] => data
 )

完成此操作后,在您的数组中添加一个城市将是微不足道的:

  • 如果数组中存在国家,则添加到国家,否则将其添加到数组并停止
  • [添加到国家:]如果该国存在该省,则添加该省,否则将该省添加到数组中并停止
  • 城市,郊区也一样......你明白了
于 2012-07-09T10:26:51.913 回答