Home >Backend Development >PHP Tutorial >PHP Infinitus Classification Skillfully Uses Reference Spanning Tree_PHP Tutorial

PHP Infinitus Classification Skillfully Uses Reference Spanning Tree_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:381132browse

First look at the code implementation

function generateTree($items){
    $tree = array();
    foreach($items as $item){
        if(isset($items[$item['pid']])){
            $items[$item['pid']]['son'][] = &$items[$item['id']];
        }else{
            $tree[] = &$items[$item['id']];
        }
    }
    return $tree;
}
$items = array(
    1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
    2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
    3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
    4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
    5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);
print_r(generateTree($items));

Output results

Array
(
    [0] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => 安徽省
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [pid] => 1
                            [name] => 合肥市
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [pid] => 3
                                            [name] => 长丰县
                                        )
 
                                )
 
                        )
 
                    [1] => Array
                        (
                            [id] => 5
                            [pid] => 1
                            [name] => 安庆市
                        )
 
                )
 
        )
 
    [1] => Array
        (
            [id] => 2
            [pid] => 0
            [name] => 浙江省
        )

)

It's really great, the code is concise and refined, no recursion is needed, and the execution speed is fast. I saw this on a website by chance. I thought it was very useful so I saved it and shared it with everyone.

---------------------------------------- The box below was proposed by the previous blogger Question, I don’t understand what it means. Sorry! ----------------------------------------

The above spanning tree method can also be reduced to 5 lines:
上面生成树方法还可以精简到5行:
function generateTree($items){
    foreach($items as $item)
        $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
    return isset($items[0]['son']) ? $items[0]['son'] : array();
}
上面这种无限极分类数据树形结构化的方法值得借鉴。但是我觉得这段代码实际用途并不明显啊,你想取出格式化的树形数据还是要递归啊:
/**
 * 如何取数据格式化的树形数据
 */
$tree = generateTree($items);
function getTreeData($tree){
    foreach($tree as $t){
        echo $t['name'].'<br>';
        if(isset($t['son'])){
            getTreeData($t['son']);
        }
    }
}
getTreeData($tree);

The above tree-structured method of Infinitus classification data is worth learning from. But I think the actual use of this code is not obvious. If you want to take out the formatted tree data, you still have to recurse:
I don’t understand why he still needs to take it out recursively. Wouldn’t it be good to output the return value of generateTree() as json to the front end?

http://www.bkjia.com/PHPjc/762936.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/762936.htmlTechArticle
First look at the code implementation function generateTree($items){ $tree = array(); foreach($items as $ item){ if(isset($items[$item['pid']])){ $items[$item['pid']]['son'][] = $items[$item['id'] ]...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn