0

I am trying to create a tree ordered select box from an array of category, subcategory data. This is the array

Array
(
    [0] => Array
        (
            [id] => 6
            [cata_key] => 32e9c75e38d2a1d77b2b49b2
            [cata_name] => Road
            [app_key] => b80e0935b348da61b2a807ff
            [parentid] => 0
        )

[1] => Array
    (
        [id] => 7
        [cata_key] => 56bae4297efcbf796b230a99
        [cata_name] => River
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 0
    )

[2] => Array
    (
        [id] => 8
        [cata_key] => 1c748603f36105b921b54165
        [cata_name] => Air
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 0
    )

[3] => Array
    (
        [id] => 9
        [cata_key] => 780c3eb53264d5c33a26d49f
        [cata_name] => Cars
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 6
    )

[4] => Array
    (
        [id] => 10
        [cata_key] => 111cd06b1575457f665d460e
        [cata_name] => Hyundai
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 9
    )

[5] => Array
    (
        [id] => 11
        [cata_key] => 26ca0e1622c11ed2628eaadf
        [cata_name] => Maruthi
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 9
    )

[6] => Array
    (
        [id] => 12
        [cata_key] => 7fc72e49fe67d0b777648f54
        [cata_name] => Boat
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 7
    )

[7] => Array
    (
        [id] => 13
        [cata_key] => b6125c73f6d7c153f45e133e
        [cata_name] => Ship
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 7
    )

[8] => Array
    (
        [id] => 14
        [cata_key] => 38c749056a9f583e284e509b
        [cata_name] => 2 Seater
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 12
    )

[9] => Array
    (
        [id] => 15
        [cata_key] => 6ebfcad4d4e05e84d333b7e5
        [cata_name] => 10 Seater
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 12
    )

[10] => Array
    (
        [id] => 16
        [cata_key] => 49973084640af71ad61e634b
        [cata_name] => Bikes
        [app_key] => b80e0935b348da61b2a807ff
        [parentid] => 6
    )
)

in which the parentid points to the parent category. The resultant array should be in the order of

Road
->Cars
    -> Hyndai
    -> Maruthi
-> Bikes
River
-> Boats
    -> 2 seater
    -> 10 seater
-> Ship
Air
4

1 回答 1

1
<?php
$categories = array(
    array('id' => 1,  'parent' => 0, 'cata_name' => 'Road'),
    array('id' => 2,  'parent' => 1, 'name' => 'Cars'),
    array('id' => 3,  'parent' =>2, 'name' => 'Maruthi'),
    array('id' => 4,  'parent' => 2, 'name' => 'Hyundai')
);

function categoriesToTree(&$categories) {

    $map = array(
        0 => array('subcategories' => array())
    );

    foreach ($categories as &$category) {
        $category['subcategories'] = array();
        $map[$category['id']] = &$category;
    }

    foreach ($categories as &$category) {
        $map[$category['parent']]['subcategories'][] = &$category;
    }

    return $map[0]['subcategories'];

}



$tree = categoriesToTree($categories);



echo "=== TREE ===\n";
echo "<pre>"; print_r ($tree);
?>
于 2013-04-18T06:34:20.507 回答