[ create a new paste ] login | about

Link: http://codepad.org/fxCTgsw5    [ raw code | output | fork | 14 comments ]

PHP, pasted on Oct 6:
<?php

$flat = array(
    "7" => array(
        "id" => 7,
        "parent" => 6
    ),
    "6" => array(
        "id" => 6,
        "parent" => 5
    ),
    "5" => array(
        "id" => 5,
        "parent" => 4
    ),
    "4" => array(
        "id" => 4,
        "parent" => 0
    ),
    "3" => array(
        "id" => 7,
        "parent" => 2
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

# transform $flat into a tree:
foreach($flat as $id => &$value)
{
    # check if there is a patrent
    if ($parentId = $value['parent'])
    {
        $flat[$parentId][0][$id] =& $value; # add child to parent
        unset($flat[$id]); # remove from topmost level
    }
}
unset($value); # remove iterator reference
print_r($flat); # your tree


Output:
Array
(
    [4] => Array
        (
            [id] => 4
            [parent] => 0
            [0] => Array
                (
                    [5] => Array
                        (
                            [id] => 5
                            [parent] => 4
                            [0] => Array
                                (
                                    [6] => Array
                                        (
                                            [id] => 6
                                            [parent] => 5
                                            [0] => Array
                                                (
                                                    [7] => Array
                                                        (
                                                            [id] => 7
                                                            [parent] => 6
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 7
            [parent] => 0
            [0] => Array
                (
                    [2] => Array
                        (
                            [id] => 7
                            [parent] => 1
                            [0] => Array
                                (
                                    [3] => Array
                                        (
                                            [id] => 7
                                            [parent] => 2
                                        )

                                )

                        )

                )

        )

)


Create a new paste based on this one


Comments:
posted by abin on Jan 31
sadsa
reply
posted by abin on Jan 31
trytrytr
reply
posted by abin on Jan 31
tryryry
reply
posted by abin on Jan 31
rtyrtyrt
reply
posted by abin on Jan 31
rtyrtyrty
reply
posted by abin on Jan 31
fghdfhdfgh
reply
posted by abin on Jan 31
rtyrtfgh
reply
posted by dina10 on Oct 30
dina10
reply
posted by abin on Jan 31
iuouoiouio
reply
posted by abin on Jan 31
poipiooiu
reply
posted by abin on Jan 31
nmn,jk
reply
posted by Cherep23 on Jul 27
fsdf
reply
posted by Cherep23 on Jul 27
sdfdf
reply
posted by Cherep23 on Jul 27
sdfd
reply