-1

我有一个数组,我想按顺序重新排列父子。在我的数组中有唯一值“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 很陌生。

4

1 回答 1

1

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

// 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
                                )

                        )

                )

        )

)
于 2013-01-14T13:02:56.357 回答