如何在php中创建一个类别子类的树

时间:2013-04-18 06:18:55

标签: php tree

我正在尝试从类别,子类别数据的数组中创建树有序选择框。这是数组

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

其中parentid指向父类别。结果数组应该是

的顺序
Road
->Cars
    -> Hyndai
    -> Maruthi
-> Bikes
River
-> Boats
    -> 2 seater
    -> 10 seater
-> Ship
Air

1 个答案:

答案 0 :(得分: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);
?>