如何安排像菜单顺序的数组

时间:2013-01-14 11:42:39

标签: php arrays

我有一个数组,我想按顺序重新安排父母对孩子。在我的数组中有独特的值'table_id'&值'parent_table'。所以parent_table将检查是否存在“table_id”。如果存在,它将在'table_id'下面。这是代码:

    Array
(
[0] => Array
(
[table_id] => 7
[table_name] => Macro
[parent_table] => 1
)
[1] => Array
(
[table_id] => 4
[table_name] => Dise
[parent_table] => 7
)
[2] => Array
(
[table_id] => 5
[table_name] => Cox
[parent_table] => 7
)
[3] => Array
(
[table_id] => 6
[table_name] => Ripo
[parent_table] => 4
)
)

示例输出:

 Array
       (
         [1] => Array
                    (
                    [table_id] => 7
                    [table_name] => Macro
                    [parent_table] => 1
                    [7] => Array
                               (
                               [table_id] => 4
                               [table_name] => Dise
                               [parent_table] => 7
                               [4] => Array
                                          (
                                           [table_id] => 6
                                           [table_name] => Ripo
                                           [parent_table] => 4
                                           )
                                )
                     [7] => Array
                                 (
                                 [table_id] => 5
                                 [table_name] => Cox
                                 [parent_table] => 7
                                 )
               )
)

请提供一些想法,因为我对php很新。

1 个答案:

答案 0 :(得分:1)

您可以使用几个foreach循环和references

// Build a new array, with nodes indexed by table_id
$byID = array();
foreach ($arr as $node) {
    $byID[$node['table_id']] = $node;
}

// Append child nodes to their parents' child_tables arrays
foreach ($byID as &$node) {
    if (isset($node['parent_table'])) {
        $byID[$node['parent_table']]['child_tables'][] =& $node;
    }
}

然后整个树由$byID[1]表示:

Array
(
    [child_tables] => Array
        (
            [0] => Array
                (
                    [table_id] => 7
                    [table_name] => Macro
                    [parent_table] => 1
                    [child_tables] => Array
                        (
                            [0] => Array
                                (
                                    [table_id] => 4
                                    [table_name] => Dise
                                    [parent_table] => 7
                                    [child_tables] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [table_id] => 6
                                                    [table_name] => Ripo
                                                    [parent_table] => 4
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [table_id] => 5
                                    [table_name] => Cox
                                    [parent_table] => 7
                                )

                        )

                )

        )

)