Home  >  Article  >  Backend Development  >  How to implement Infinitus classification in PHP? (with code)

How to implement Infinitus classification in PHP? (with code)

不言
不言Original
2018-08-13 15:09:142217browse

The content of this article is about how to implement Infinitus classification in PHP? (Attached is the code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I used Infinitus Classification when writing the project. Due to the special reasons of the project, the menu bar needs to call this method multiple times. Problems will occur when using static variables to save arrays, so I chose the reference method.

public function getTree($array, $pid = 0, $level = 0, &$list)
    {
        foreach ($array as $key => $value) {            
        //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['parentid'] == $pid) {                
            //父节点为根节点的节点,级别为0,也就是第一级
                $value['level'] = $level;                
                //把数组放到list中
                $list[] = $value;                
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);                
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level + 1, $list);

            }
        }        
        return $list;
    }

Related recommendations:

How to achieve the classification tree effect in php? (Code attached)

How to use php to get the size of the file to judge? (code example)

The above is the detailed content of How to implement Infinitus classification in PHP? (with code). For more information, please follow other related articles on the PHP Chinese website!

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