SoFunction
Updated on 2025-04-09

PHP Unlimited Classification

I haven't used Infinitus classification for a while, and unfortunately I used it again today, so I went to rummage through the box and review the past. In order to avoid trouble in the future, I posted it here.

<?php
/**
  * Unlimited Classification Class
  */
class Category{
 /**
   * Return a one-dimensional array
   * @param [type] $cate the array to be recursive
   * @param string $html Indentation symbol to be displayed before child classification.  Default '-'
   * @param integer $pid Parent Classification ID.  Default is 0, indicating the top-level classification
   * @param integer $level level, display sufficient indentation with $html.  Default is 1, indicating the top-level classification
   * @return [type] [description]
   */
 static public function unlimitedForLevel($cate, $html = '─', $pid = 0, $level = 1){
  $arr = array();
  foreach($cate as $v){
   if($v['pid'] == $pid){
    $v['level'] = $level;
    $v['html'] = str_repeat($html, $level - 1);
    $arr[] = $v;
    $arr = array_merge($arr, $this->unlimitedForLevel($cate, $html, $v['id'], $level + 1));
   }
  }
  return $arr;
 }
 /**
   * Return a multi-dimensional array
   * @param [type] $cate the array to be recursive
   * @param string $name key of child classification in parent classification array
   * @param integer $pid Parent Classification ID.  Default is 0, indicating top-level classification
   * @return [type] [description]
   */
 static public function unlimitedForlayer($cate, $name = 'child', $pid = 0){
  $arr = array();
  foreach($cate as $v){
   if( $v['pid'] == $pid){
    $v[$name] = self::unlimitedForlayer($cate, $name, $v['id']);
    $arr[] = $v;
   }
  }
  return $arr;
 }
 /**
   * Pass the child classification ID to return all parent classifications
   * @param [type] $cate the array to be recursive
   * @param [type] $id Subcategory ID
   * @return [type] [description]
   */
 static public function getParents($cate, $id){
  $arr = array();
  foreach($cate as $v){
   if($v['id'] == $id){
    $arr[] = $v;
    $arr = array_merge(self::getParents($cate, $v['pid']), $arr);
   }
  }
  return $arr;
 }
 /**
   * Pass the parent class ID to return all child class IDs
   * @param [type] $cate the array to be recursive
   * @param [type] $pid Parent Classification ID
   * @return [type] [description]
   */
 static public function getChildrenId($cate, $pid){
  $arr = array();
  foreach($cate as $v){
   if($v['pid'] == $pid){
    $arr[] = $v['id'];
    $arr = array_merge($arr, self::getChildrenId($cate, $v['id']));
   }
  }
  return $arr;
 }
 /**
   * Pass the parent class ID to return all child classes
   * @param [type] $cate the array to be recursive
   * @param [type] $pid Parent Classification ID
   * @return [type] [description]
   */
 static public function getChildren($cate, $pid){
  $arr = array();
  foreach($cate as $v){
   if($v['pid'] == $pid){
    $arr[] = $v;
    $arr = array_merge($arr, self::getChildren($cate, $v['id']));
   }
  }
  return $arr;
 }
}
?>

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!