从单维数组创建分层数组

时间:2013-03-22 09:03:49

标签: php arrays recursion multidimensional-array jstree

我想从从数据库获得的单维数组中创建一个层次结构数组。语言是PHP。

在下面提到的示例中,密钥id -3表示它是根节点。

输入数据:

Array
(
    [-2] => Array
            (
                [data] => A-2
                [attr] => Array
                (
                    [id] => -2
                    [parent_id] => -3
                    [title] => A-2
                )
                [state] => open
            }
    [-1] => Array
            (
                [data] => A-1
                [attr] => Array
                (
                    [id] => -1
                    [parent_id] => -2
                    [title] => A-1
                )
                [state] => open
            }
    [0] => Array
            (
                [data] => A0
                [attr] => Array
                (
                    [id] => 0
                    [parent_id] => -1
                    [title] => A0
                )
                [state] => open
            }
    [1] => Array
            (
                [data] => A1
                [attr] => Array
                (
                    [id] => 1
                    [parent_id] => -1
                    [title] => A1
                )
                [state] => open
            }
    [2] => Array
            (
                [data] => A2
                [attr] => Array
                (
                    [id] => 2
                    [parent_id] => -1
                    [title] => A2
                )
                [state] => open
            }
    [3] => Array
            (
                [data] => A3
                [attr] => Array
                (
                    [id] => 3
                    [parent_id] => 2
                    [title] => A3
                )
                [state] => open
            }
    [4] => Array
            (
                [data] => A4
                [attr] => Array
                (
                    [id] => 4
                    [parent_id] => 2
                    [title] => A4
                )
                [state] => open
            }
    [5] => Array
            (
                [data] => A5
                [attr] => Array
                (
                    [id] => 5
                    [parent_id] => -2
                    [title] => A5
                )
                [state] => open
            }
}

输出预期数据:

Array
(
    [-2] => Array
    (
        [data] => A-2
        [attr] => Array
        (
            [id] => -2
            [parent_id] => -3
            [title] => A-2
        )
        [state] => open
        [children] => Array
        (
            [-1] => Array
            (
                [data] => A-1
                [attr] => Array
                (
                    [id] => -1
                    [parent_id] => -2
                    [title] => A-1
                )
                [state] => open
                [children] => Array
                (
                    [0] => Array
                    (
                        [data] => A0
                        [attr] => Array
                        (
                            [id] => 0
                            [parent_id] => -1
                            [title] => A0
                        )
                        [state] => open
                    }
                    [1] => Array
                    (
                        [data] => A1
                        [attr] => Array
                        (
                            [id] => 1
                            [parent_id] => -1
                            [title] => A1
                        )
                        [state] => open
                    }
                    [2] => Array
                    (
                        [data] => A2
                        [attr] => Array
                        (
                            [id] => 2
                            [parent_id] => -1
                            [title] => A2
                        )
                        [state] => open
                        [children] => Array
                        (
                            [3] => Array
                            (
                                [data] => A3
                                [attr] => Array
                                (
                                    [id] => 3
                                    [parent_id] => 2
                                    [title] => A3
                                )
                                [state] => open
                            }
                            [4] => Array
                            (
                                [data] => A4
                                [attr] => Array
                                (
                                    [id] => 4
                                    [parent_id] => 2
                                    [title] => A4
                                )
                                [state] => open
                            }
                        )
                    )
                )
            )
            [5] => Array
            (
                [data] => A5
                [attr] => Array
                (
                    [id] => 5
                    [parent_id] => -2
                    [title] => A5
                )
                [state] => open
            )
        )
    )
)

1 个答案:

答案 0 :(得分:0)

$a1 = array( ... your data is here ... );
$a2 = array(); # there are root nodes
$root_ids = array();

foreach($a1 as &$a)
{
    if( isset($a1[$a['attr']['parent_id']]) )
        $a1[$a['attr']['parent_id']]['children'][$a['attr']['id']] = &$a;
    else
        $a2[$a['attr']['id']] = &$a;
}

print_r( $a2 );

诀窍是使用& $ a表示法,它给我们引用而不是变量副本。基本上非根节点连接到父节点,根节点连接到结果数组。