Home > Article > Backend Development > PHP infinite classification best practices
is a very common and necessary function, almost every project has it.
Application scenarios: drop-down lists, tree lists, etc.
Front-end implementation (The front-end framework has generally been implemented, and it can be generated as long as the back-end transmits data to the front-end in the specified format)
Back-end Implementation(The following mainly talks about this kind of implementation)
The first type (recommended)
function infiniteSort($data, $showFName, $titleFName, $pidFName = 'pid', $idFName = 'id', $levelFName = 'level', $pid = 0, $level = 0) { $tree = array(); foreach ($data as $key => $value) { if ($value[$pidFName] == $pid) { $value[$levelFName] = $level; $value[$showFName] = str_repeat(' ', $level) . '|-' . $value[$titleFName]; $tree[] = $value; unset($data[$key]); $tempArr = infiniteSort($data, $showFName, $titleFName, $pidFName, $idFName, $level, $value[$idFName], $level + 1); if(!empty($tempArr)){ $tree = array_merge($tree, $tempArr); } } } return $tree; }
Note:
1, $data All data that has been sorted by asc
2, $showFName Display name Field name of the title (formatted)
3. $titleFName Field name of the title (unformatted)
4. $levelFName Level field name
5, $pidFName The field name of the parent id
6, $idFName The field name of the id
The second type (using reference variables)
/** * 无限级分类 * @param Array $treeList //接受处理完成数据的数组 * @param Array $data //数据库里获取的结果集 * @param String $level //格式化层级字段名 * @param Int $pid * @param Int $count //第几级分类 */ function tree(&$treeList, &$data, $level, $show_name, $field_name, $field_pid = 'pid', $field_id = 'id', $pid = 0, $count = 0) { foreach ($data as $key => $value) { if ($value[$field_pid] == $pid) { $value[$level] = $count; $value[$show_name] = str_repeat(' ',$count).'|-'.$value[$field_name]; $treeList[] = $value; unset($data[$key]); tree($treeList, $data, $level, $show_name, $field_name,$field_pid, $field_id, $value[$field_id], $count+1); } } }
Note:
1, $data All data that has been sorted by asc
2 , the returned infinite list data is stored in $treeList
The third type (there are restrictions on using static variables: if a request is called twice to achieve 2 Infinite level classification will cause problems, so it is not recommended)
public function getTree($list, $parent_id, $level=0) { //应该是静态的局部变量,这样才能保证,在递归调用时,所有 //的getTree方法,操作的是一个Tree空间。 static $tree = array();//保存找到的分类的数组 //遍历所有分类,通过parent_id判断,哪些是我们正在查找的 foreach($list as $row) { //判断当前所遍历的分类$row, 是否是当前需要查找的子分类 if($row['pid'] == $parent_id) { //找到了一个分类 //存起来,存哪? $row['level'] = $level; $tree[] = $row; //继续查找当前$row所代表的分类的子分类 $this->getTree($list, $row['id'], $level+1); } } return $tree; }
Note:
1, $list All data that has been sorted by asc
is a very common and necessary function, almost every project has it.
Application scenarios: drop-down lists, tree lists, etc.
Front-end implementation (The front-end framework has generally been implemented, and it can be generated as long as the back-end transmits data to the front-end in the specified format)
Back-end Implementation(The following mainly talks about this kind of implementation)
The first type (recommended)
function infiniteSort($data, $showFName, $titleFName, $pidFName = 'pid', $idFName = 'id', $levelFName = 'level', $pid = 0, $level = 0) { $tree = array(); foreach ($data as $key => $value) { if ($value[$pidFName] == $pid) { $value[$levelFName] = $level; $value[$showFName] = str_repeat(' ', $level) . '|-' . $value[$titleFName]; $tree[] = $value; unset($data[$key]); $tempArr = infiniteSort($data, $showFName, $titleFName, $pidFName, $idFName, $level, $value[$idFName], $level + 1); if(!empty($tempArr)){ $tree = array_merge($tree, $tempArr); } } } return $tree; }
Note:
1, $data All data that has been sorted by asc
2,$ showFName Display the field name of the name (formatted)
3, $titleFName The field name of the title (unformatted)
4, $levelFName Hierarchical field name
5, $pidFName Field name of parent id
6, $idFName Field name of id
Second type (using reference variables)
/** * 无限级分类 * @param Array $treeList //接受处理完成数据的数组 * @param Array $data //数据库里获取的结果集 * @param String $level //格式化层级字段名 * @param Int $pid * @param Int $count //第几级分类 */ function tree(&$treeList, &$data, $level, $show_name, $field_name, $field_pid = 'pid', $field_id = 'id', $pid = 0, $count = 0) { foreach ($data as $key => $value) { if ($value[$field_pid] == $pid) { $value[$level] = $count; $value[$show_name] = str_repeat(' ',$count).'|-'.$value[$field_name]; $treeList[] = $value; unset($data[$key]); tree($treeList, $data, $level, $show_name, $field_name,$field_pid, $field_id, $value[$field_id], $count+1); } } }
Note:
1, $data All data that has been sorted by asc
2. The returned infinite-level list data is stored in $treeList
The third type (there are restrictions on using static variables: if one request calls Implementing two infinite levels of classification twice will cause problems, so it is not recommended)
public function getTree($list, $parent_id, $level=0) { //应该是静态的局部变量,这样才能保证,在递归调用时,所有 //的getTree方法,操作的是一个Tree空间。 static $tree = array();//保存找到的分类的数组 //遍历所有分类,通过parent_id判断,哪些是我们正在查找的 foreach($list as $row) { //判断当前所遍历的分类$row, 是否是当前需要查找的子分类 if($row['pid'] == $parent_id) { //找到了一个分类 //存起来,存哪? $row['level'] = $level; $tree[] = $row; //继续查找当前$row所代表的分类的子分类 $this->getTree($list, $row['id'], $level+1); } } return $tree; }
Note:
1, $list All data that has been sorted by asc
For more articles related to PHP infinite classification best practices, please pay attention to the PHP Chinese website!