Home >Backend Development >PHP Tutorial >Thinkphp's list_to_tree implements unlimited classification to list all nodes_PHP tutorial

Thinkphp's list_to_tree implements unlimited classification to list all nodes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:27922browse

Thinkphp's list_to_tree implements unlimited classification to list all nodes

list_to_tree is very convenient to use, please refer to the manual for details. Because I need to list all nodes at the same time when using it, I wrote a recursive function and put it out for your reference.
public function index(){
    Load('extend');            //加载扩展方法
    $Category=D('Category');
    $list=$Category->order('sort desc')->select();//实现同级节点排序
    $list=list_to_tree($list,'id','fid'); //详细参数见手册
    $list=$this->findChild($list);    
    dump($list);
}
protected  function findChild($arr){
        static $tree=array();
        foreach ($arr as $key=>$val){
                $tree[]=$val;
                if (isset($val['_child'])){
                    $this->findChild($val['_child']);
                }        
            }
    return $tree;
}



/**
 * 把返回的数据集转换成Tree
 * @access public
 * @param array $list 要转换的数据集
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 * @return array
 */
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0) {
    // 创建Tree
    $tree = array();
    if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
    }
    return $tree;
}

/**
 * 对查询结果集进行排序
 * @access public
 * @param array $list 查询结果
 * @param string $field 排序的字段名
 * @param array $sortby 排序类型
 * asc正向排序 desc逆向排序 nat自然排序
 * @return array
 */
function list_sort_by($list,$field, $sortby='asc') {
   if(is_array($list)){
       $refer = $resultSet = array();
       foreach ($list as $i => $data)
           $refer[$i] = &$data[$field];
       switch ($sortby) {
           case 'asc': // 正向排序
                asort($refer);
                break;
           case 'desc':// 逆向排序
                arsort($refer);
                break;
           case 'nat': // 自然排序
                natcasesort($refer);
                break;
       }
       foreach ( $refer as $key=> $val)
           $resultSet[] = &$list[$key];
       return $resultSet;
   }
   return false;
}

/**
 * 在数据列表中搜索
 * @access public
 * @param array $list 数据列表
 * @param mixed $condition 查询条件
 * 支持 array('name'=>$value) 或者 name=$value
 * @return array
 */
function list_search($list,$condition) {
    if(is_string($condition))
        parse_str($condition,$condition);
    // 返回的结果集合
    $resultSet = array();
    foreach ($list as $key=>$data){
        $find   =   false;
        foreach ($condition as $field=>$value){
            if(isset($data[$field])) {
                if(0 === strpos($value,'/')) {
                    $find   =   preg_match($value,$data[$field]);
                }elseif($data[$field]==$value){
                    $find = true;
                }
            }
        }
        if($find)
            $resultSet[]     =   &$list[$key];
    }
    return $resultSet;
}



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847865.htmlTechArticleThinkphp's list_to_tree implements infinite classification to list all nodes. list_to_tree is very convenient to use. You can check the manual for details. Because I need to list all nodes at the same time when using it...
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