How to control the depth of php infinite pole classification

Viewed 38

Here is my array:

 $list = array(
        array('id' => 3, 'name' => 'Phones', 'parent_id' => 0,),
        array('id' => 256, 'name' => 'Accessories', 'parent_id' => 0,),
        array('id' => 308, 'name' => 'Appliances', 'parent_id' => 0,),
        array('id' => 1057, 'name' => 'Smart', 'parent_id' => 0,),
        array('id' => 1065, 'name' => 'Smart Phones', 'parent_id' => 3,),
        array('id' => 1066, 'name' => 'Feature Phones', 'parent_id' => 3,),
        array('id' => 1069, 'name' => 'Samsung', 'parent_id' => 1065,),
        array('id' => 1070, 'name' => 'Apple', 'parent_id' => 1065,),
        array('id' => 1072, 'name' => 'Apple', 'parent_id' => 1066,),
        array('id' => 1075, 'name' => 'Tablets', 'parent_id' => 0,),
        array('id' => 1076, 'name' => 'Samsung', 'parent_id' => 1066,),
        array('id' => 1077, 'name' => 'All Brands', 'parent_id' => 1075,),
        array('id' => 1078, 'name' => 'Samsung', 'parent_id' => 1077,),
        array('id' => 1079, 'name' => 'Protector', 'parent_id' => 256,),
        array('id' => 1080, 'name' => 'Power', 'parent_id' => 256,),
        array('id' => 1081, 'name' => 'Cable', 'parent_id' => 256,),
        array('id' => 1082, 'name' => 'Memory', 'parent_id' => 256,),
    );

This is the code I categorized:

function  quote_make_tree($list,$deep=1, $root = 0)
{
    $tree = $packData = [];
    foreach ($list as $row) {
        $packData[$row['id']] = $row;
    }

    foreach ($packData as $key => $val) {
        if ($val['parent_id'] == $root) {
            $tree[] = &$packData[$key];
        } else {
            $packData[$val['parent_id']]['children'][] = &$packData[$key];

        }

    }
    return $tree;
}

I want to add a deep parameter $deep to quote_make_tree to control the depth of classification.

The result I want: If $deep = 1 Get only data with parent_id = 0:

 $one = array(
        0 => array('id' => 3, 'name' => 'Phones', 'parent_id' => 0,),
        1 => array('id' => 256, 'name' => 'Accessories', 'parent_id' => 0,),
        2 => array('id' => 308, 'name' => 'Appliances', 'parent_id' => 0,),
        3 => array('id' => 1057, 'name' => 'Smart', 'parent_id' => 0,),
        4 => array('id' => 1075, 'name' => 'Tablets', 'parent_id' => 0,),
    );

If $deep = 2,Get level 2 classification below level 1 classification,if $deep=3,Get all 1,2,3 classifications

I have tried get all the data first, then delete the data according to $deep.here is demo,thanks folks

1 Answers

This might be easier to solve using a recursive method. This allows you to check the depth and if you want more details then it will call itself to add in more layers. Each time calling itself you just take one off the depth...

function  quote_make_tree($list, $deep=1, $root = 0) {
    $output = [];
    foreach ( $list as $listItem )  {
        if ( $listItem['parent_id'] == $root ) {
            if ( $deep > 1 )    {
                $listItem['children'] = quote_make_tree($list, $deep-1, $listItem['id']);
            }
            $output[] = $listItem;
        }
    }
    return $output;
}
Related