重建多维数组数据php

时间:2017-07-13 10:10:21

标签: php arrays multidimensional-array

我有一个像这样的数组

$array = array(array("1" => "a",
                        "2" => "hello a1",
                        "3" => "ab",
                        "4" => "ab1",
                        "5" => "hello ab1"),
                array("1" => "a",
                        "2" => "hello a1",
                        "3" => "ab",
                        "4" => "ab2",
                        "5" => "hello ab2"),
                array("1" => "a",
                        "2" => "hello a1",
                        "3" => "ac",
                        "4" => "ac2",
                        "5" => "hello ac2"),
                array("1" => "b",
                        "2" => "hello b1",
                        "3" => "bb",
                        "4" => "bb1",
                        "5" => "hello bb1"),
                array("1" => "b",
                        "2" => "hello b1",
                        "3" => "bb",
                        "4" => "bb2",
                        "5" => "hello bb2"),
                array("1" => "b",
                        "2" => "hello b1",
                        "3" => "bc",
                        "4" => "bc3",
                        "5" => "hello bc3"),
        );

我想获得此输出

$final = array(array("1" => "a",
                        "2" => "hello a1",
                        "3" => array(array("1a" =>  "ab",
                                            "2a" => array(array("1b" => "ab1",
                                                                "2b" => "hello ab1"),
                                                            array("1b" => "ab2",
                                                                    "2b" => "hello ab2")
                                        ),
                                    array("1a" =>  "ac",
                                            "2a" => array(array("1b" => "ac2",
                                                                "2b" => "hello ac2")
                                        )
                                    )
                                )
                            )
                        ),
                array("1" => "b",
                        "2" => "hello b1",
                        "3" => array(array("1a" =>  "bb",
                                            "2a" => array(array("1b" => "bb1",
                                                                "2b" => "hello bb1"),
                                                            array("1b" => "bb2",
                                                                    "2b" => "hello bb2")
                                        ),
                                    array("1a" =>  "bc",
                                            "2a" => array(array("1b" => "bc3",
                                                                "2b" => "hello bc3")
                                        )
                                    )
                                )
                            )
                        ),
                    );

如您所见,父值是a和b。子父母是ab,ac,bb,bc。 儿童是ab1,ab2,ac2,bb1,bb2,bc3。我希望通过以下方式匹配数组: 父母 - >子母 - >孩子

1 个答案:

答案 0 :(得分:0)

你可以像这样简单

foreach ($array as $key) {
 foreach ($key as $value) {
   echo $value;
   echo "<br>";
 }
}