从父/子数组列表中获取子进程

时间:2012-10-05 09:48:07

标签: php

我有一个关于如何从父/子数组列表中获取某些字段的严重问题

我的清单:

Array
(
[356] => Array
    (
        [label] => Monde entier
        [childrens] => Array
            (
                [355] => Array
                    (
                        [label] => International (hors U.E)
                        [childrens] => Array
                            (
                            )

                    )

                [354] => Array
                    (
                        [label] => Europe
                        [childrens] => Array
                            (
                                [353] => Array
                                    (
                                        [label] => France entière
                                        [childrens] => Array
                                            (
                                                [146] => Array
                                                    (
                                                        [label] => Nord
                                                        [childrens] => Array
                                                            (
                                                                [161] => Array
                                                                    (
                                                                        [label] => Champagne-Ardenne
                                                                        [childrens] => Array
                                                                            (
                                                                                [258] => Array
                                                                                    (
                                                                                        [label] => Ardennes
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [260] => Array
                                                                                    (
                                                                                        [label] => Aube
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [302] => Array
                                                                                    (
                                                                                        [label] => Marne
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [303] => Array
                                                                                    (
                                                                                        [label] => Haute-Marne
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                            )

                                                                    )

                                                                [169] => Array
                                                                    (
                                                                        [label] => Nord-Pas-de-Calais
                                                                        [childrens] => Array
                                                                            (
                                                                                [310] => Array
                                                                                    (
                                                                                        [label] => Nord
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [313] => Array
                                                                                    (
                                                                                        [label] => Pas-de-Calais
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                            )

                                                                    )

                                                                [173] => Array
                                                                    (
                                                                        [label] => Picardie
                                                                        [childrens] => Array
                                                                            (
                                                                                [252] => Array
                                                                                    (
                                                                                        [label] => Aisne
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [311] => Array
                                                                                    (
                                                                                        [label] => Oise
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [331] => Array
                                                                                    (
                                                                                        [label] => Somme
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                    )

                                              ......

                                                [152] => Array
                                                    (
                                                        [label] => Dom-Tom
                                                        [childrens] => Array
                                                            (
                                                                [178] => Array
                                                                    (
                                                                        [label] => Dom-Tom
                                                                        [childrens] => Array
                                                                            (
                                                                                [347] => Array
                                                                                    (
                                                                                        [label] => Guadeloupe
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [348] => Array
                                                                                    (
                                                                                        [label] => Martinique
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [349] => Array
                                                                                    (
                                                                                        [label] => Guyane
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [350] => Array
                                                                                    (
                                                                                        [label] => Réunion
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                                [352] => Array
                                                                                    (
                                                                                        [label] => Autres DOM-TOM 
                                                                                        [childrens] => Array
                                                                                            (
                                                                                            )

                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

所以我想创建一个函数来返回一个包含这样列表的数组:

enter image description here

和我的职能:

public function getListArray(array $config['place']) {



.....

return $result;

}

1 个答案:

答案 0 :(得分:1)

这应该这样做

function getListArray($array) {
  static $level = -1;
  static $result = array();
  $level++;
  foreach ($array as $arr) {
    $result[] = str_repeat ('-', $level).$arr['label'];
    if (is_array($arr['childrens'])) {
      getListArray($arr['childrens']);
    }
  }
  $level--;

  return $result;
}