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

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

不言
Release: 2023-04-03 18:38:01
Original
2284 people have browsed it

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;
    }
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template