PHP将平面数组转换为多维关系数组

时间:2012-11-15 13:53:01

标签: php arrays recursion multidimensional-array foreach

  

可能重复:
  Recursive function to generate multidimensional array from database result

考虑以下数组。

    Array
(
    [0] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 0
            [0] => string:val

        )

    [1] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 1
            [0] => string:val

        )

    [2] => Array
        (
            [Parent_ID] => 1
            [Child_ID] => 2
            [0] => string:val

        )

    [3] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 3
            [0] => string:val

        )

我需要将其变成以下内容。

 Array
(
    [0] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 0
            [0] => string:val

        )

    [1] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 1
            [0] => string:val


                [2] => Array
                   (
                    [Parent_ID] => 1
                    [Child_ID] => 2
                    [0] => string:val

                    ) 
                   )

    [3] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 3
            [0] => string:val

        )

除此之外,儿童还可以拥有无​​限的孩子​​。并非每个索引都具有相同数量的键和值。但是所有人都有父/子密钥/值。

我从这里和其他地方尝试了很多解决方案,似乎无法做到正确。

0 个答案:

没有答案