Thinkphp的list_to_tree 实现无限级分类列出所有节点
list_to_tree 使用起来十分方便,详细可查看手册。因为我在用的时候需要同时列出所有节点,所以写了一个递归函数,拿出来供大家参考。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; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
