Home > Backend Development > PHP Tutorial > How to implement infinite classification in PHP

How to implement infinite classification in PHP

墨辰丷
Release: 2023-03-27 15:44:02
Original
1053 people have browsed it

This article mainly introduces the relevant knowledge of PHP infinite classification, which has a good reference value. Let’s take a look at it with the editor below

I haven’t used Infinitus Classification for a while, but unfortunately I used it again today, so I went through the box to review the past. In order to avoid trouble finding it in the future, I post it here.

<?php
/**
 * 无限级分类 类
 */
class Category{
 /**
  * 返回一维数组
  * @param [type] $cate 要递归的数组
  * @param string $html 子级分类前要显示的缩进符号。默认 &#39;─&#39;
  * @param integer $pid 父级分类ID。默认为 0,表示顶级分类
  * @param integer $level level级,配合 $html 显示足够的缩进。默认为 1,表示顶级分类
  * @return [type]   [description]
  */
 static public function unlimitedForLevel($cate, $html = &#39;─&#39;, $pid = 0, $level = 1){
  $arr = array();
  foreach($cate as $v){
   if($v[&#39;pid&#39;] == $pid){
    $v[&#39;level&#39;] = $level;
    $v[&#39;html&#39;] = str_repeat($html, $level - 1);
    $arr[] = $v;
    $arr = array_merge($arr, $this->unlimitedForLevel($cate, $html, $v[&#39;id&#39;], $level + 1));
   }
  }
  return $arr;
 }
 /**
  * 返回多维数组
  * @param [type] $cate 要递归的数组
  * @param string $name 子级分类在父分类数组中的 key
  * @param integer $pid 父级分类ID。默认为0,表示顶级分类
  * @return [type]  [description]
  */
 static public function unlimitedForlayer($cate, $name = &#39;child&#39;, $pid = 0){
  $arr = array();
  foreach($cate as $v){
   if( $v[&#39;pid&#39;] == $pid){
    $v[$name] = self::unlimitedForlayer($cate, $name, $v[&#39;id&#39;]);
    $arr[] = $v;
   }
  }
  return $arr;
 }
 /**
  * 传递子分类ID返回所有父级分类
  * @param [type] $cate 要递归的数组
  * @param [type] $id 子分类ID
  * @return [type]  [description]
  */
 static public function getParents($cate, $id){
  $arr = array();
  foreach($cate as $v){
   if($v[&#39;id&#39;] == $id){
    $arr[] = $v;
    $arr = array_merge(self::getParents($cate, $v[&#39;pid&#39;]), $arr);
   }
  }
  return $arr;
 }
 /**
  * 传递父级分类ID返回所有子分类ID
  * @param [type] $cate 要递归的数组
  * @param [type] $pid 父级分类ID
  * @return [type]  [description]
  */
 static public function getChildrenId($cate, $pid){
  $arr = array();
  foreach($cate as $v){
   if($v[&#39;pid&#39;] == $pid){
    $arr[] = $v[&#39;id&#39;];
    $arr = array_merge($arr, self::getChildrenId($cate, $v[&#39;id&#39;]));
   }
  }
  return $arr;
 }
 /**
  * 传递父级分类ID返回所有子级分类
  * @param [type] $cate 要递归的数组
  * @param [type] $pid 父级分类ID
  * @return [type]  [description]
  */
 static public function getChildren($cate, $pid){
  $arr = array();
  foreach($cate as $v){
   if($v[&#39;pid&#39;] == $pid){
    $arr[] = $v;
    $arr = array_merge($arr, self::getChildren($cate, $v[&#39;id&#39;]));
   }
  }
  return $arr;
 }
}
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php data access add, delete, modify and check operations_php skills

PHP Implemented encryption and decryption processing class_php skills

##PHPtime(), date(), mktime() difference introduction_php basics

The above is the detailed content of How to implement infinite classification in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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