0

データベースから取得した一次元配列から階層配列を作成したい。言語は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
            )
        )
    )
)
4

1 に答える 1

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 表記を使用することです。基本的に非ルート ノードは親に接続され、ルート ノードは結果配列に移動します。

于 2013-03-22T21:01:28.440 に答える