Home > Backend Development > PHP Tutorial > How to improve the efficiency of PHP infinite classification query (using arrays and recursion)

How to improve the efficiency of PHP infinite classification query (using arrays and recursion)

PHP中文网
Release: 2023-02-28 20:48:02
Original
1290 people have browsed it

How to improve the efficiency of phpinfinite classificationquery (using array and recursion)

<?php
class Tree {
    /**
     * 从数据库查询出的所有分类信息
     * @var array
     * by:bbs.it-home.org
     */
    var $arr;
    /**
     * 如下格式
     * var $arr = array(
     1 => array(‘id’=>’1′,’parentid’=>0,’name’=>’一级栏目一’),
     2 => array(‘id’=>’2′,’parentid’=>0,’name’=>’一级栏目二’),
     3 => array(‘id’=>’3′,’parentid’=>1,’name’=>’二级栏目一’),
     );*/
    /**
     * 输出结构
     * @var array
     */
    var $tree = array();
    /**
     * 树形递归的深度
     * @var int
     */
    var $deep = 1;
    /**
     * 生成树形的修饰符号
     * @var array
     */
    var $icon = array(&#39;│&#39;,&#39;├&#39;,&#39;└&#39;);
    /**
     * 生成指定id的下级树形结构
     * @param int $rootid 要获取树形结构的id
     * @param string $add 递归中使用的前缀
     * @param bool $parent_end 标识上级分类是否是最后一个
     */
    function getTree($rootid = 0,$add = "",$parent_end =true){
        $is_top = 1;
        $child_arr = $this->getChild($rootid);
        if(is_array($child_arr)){
            $cnt = count($child_arr);
            foreach($child_arr as $key => $child){
                $cid = $child[&#39;id&#39;];
                $child_child = $this->getChild($cid);
                if($this->deep >1){
                    if($is_top == 1 && $this->deep > 1){
                        $space = $this->icon[1];
                        if(!$parent_end)
                        $add .= $this->icon[0];
                        else $add .= "";
                    }
                    if($is_top == $cnt){
                        $space = $this->icon[2];
                        $parent_end = true;
                    }else {
                        $space = $this->icon[1];
                        $parent_end = false;
                    }
                }
                $this->tree[] = array(‘spacer’=>$add.$k.$space,
                ‘name’=>$child[&#39;name&#39;],
                ‘id’=>$cid
                );
                $is_top++;
                $this->deep++;
                if($this->getChild($cid))
                $this->getTree($cid,$add,$parent_end);
                $this->deep–;
            }
        }
        return $this->tree;
    }
    /**
     * 获取下级分类数组
     * @param int $root
     */
    function getChild($root = 0){
        $a = $child = array();
        foreach($this->arr as $id=>$a){
            if($a[&#39;parentid&#39;] == $root){
                $child[$a[&#39;id&#39;]] = $a;
            }
        }
        return $child?$child:false;
    }
    /**
     * 设置源数组
     * @param $arr
     */
    function setArr($arr = array()){
        $this->arr = $arr;
    }
}
?>
Copy after login

Save the structure into an array through one query, and then perform recursive operations on the array, which is undoubtedly huge Improved program operation efficiency. The use of the code is very simple: after getting the query structure, setArr and directly call getTree, you can get an array sorted according to the program number and with prefix modification and other information. Through the foreach array, you can get the following tree-like list: Fruit ├Banana ├Apple │├Red Fuji │└Hainan Apple └Peach Remember: During the website development process, most bottlenecks are in the database, not the php code.

The above is how to improve the efficiency of unlimited classification queries in PHP (using arrays and recursion). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template