Home  >  Article  >  Backend Development  >  PHP recursive traversal to achieve infinite classification

PHP recursive traversal to achieve infinite classification

墨辰丷
墨辰丷Original
2018-06-08 15:13:592170browse

This article mainly introduces PHP recursive traversal to achieve infinite classification. Interested friends can refer to it. I hope it will be helpful to everyone.

Infinite-level classification is a classification technique. For example, department organization, article classification, subject classification, etc. often use infinite-level classification. It can be simply understood as classification. In fact, if we think about it carefully, there are simply too many classifications in life. Clothes can be divided into men's clothing and women's clothing, tops and pants, and they can also be classified according to age groups. Classification is everywhere, and classification appears "infinite". I won’t talk about the necessity of infinite classification here.

The example of this article describes the method of php recursion to achieve infinite classification, the details are as follows:

<?php
$rows = array(
  array(
    &#39;id&#39; => 1,
    &#39;name&#39; => &#39;dev&#39;,
    &#39;parentid&#39; => 0
  ),
  array(
    &#39;id&#39; => 2,
    &#39;name&#39; => &#39;php&#39;,
    &#39;parentid&#39; => 1
  ),
  array(
    &#39;id&#39; => 3,
    &#39;name&#39; => &#39;smarty&#39;,
    &#39;parentid&#39; => 2
  ),
  array(
    &#39;id&#39; => 4,
    &#39;name&#39; => &#39;life&#39;,
    &#39;parentid&#39; => 0
  ),
  array(
    &#39;id&#39; => 5,
    &#39;name&#39; => &#39;pdo&#39;,
    &#39;parentid&#39; => 2
  ),
  array(
    &#39;id&#39; => 6,
    &#39;name&#39; => &#39;pdo-mysql&#39;,
    &#39;parentid&#39; => 5
  ),
  array(
    &#39;id&#39; => 7,
    &#39;name&#39; => &#39;java&#39;,
    &#39;parentid&#39; => 1
  )
);
// 72648
// 84072
function findChild(&$arr,$id){
  $childs=array();
   foreach ($arr as $k => $v){
     if($v[&#39;parentid&#39;]== $id){
       $childs[]=$v;
     }
  }
  return $childs;
}
function build_tree($root_id){
  global $rows;
  $childs=findChild($rows,$root_id);
  if(empty($childs)){
    return null;
  }
  foreach ($childs as $k => $v){
    $rescurTree=build_tree($v[id]);
    if( null !=  $rescurTree){ 
    $childs[$k][&#39;childs&#39;]=$rescurTree;
    }
  }
  return $childs;
}
$tree=build_tree(0);
echo memory_get_usage();
print_r($tree);
?>

Summary: The above is the entire content of this article, I hope it can be helpful to everyone Learning helps.

Related recommendations:

Definition and generation method of PHP barcode

php determines and obtains the file extension Several methods

php image file operation encapsulation class

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

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