Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to create a hierarchial array from the single dimensional array obtained from database. Language is PHP.

In the below mentioned example key id -3 indicates that it is the root node.

Input Data:

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
            }
}

Output expected data:

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
            )
        )
    )
)
share|improve this question

1 Answer 1

$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 );

The trick is to use &$a notation which gives us references instead of variable copies. Basically non-root nodes are attached to the parents, and root nodes go to result array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.