Home > Backend Development > PHP Tutorial > PHP implements unlimited classification, PHP implements level classification_PHP tutorial

PHP implements unlimited classification, PHP implements level classification_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:10:49
Original
940 people have browsed it

php realizes unlimited classification, php realizes level classification

Copy code The code is as follows:

$area = array(
array('id'=>1,'name'=>'Anhui','parent'=>0),
array('id'=>2,'name'=>'Haidian','parent'=>7),
array('id'=>3,'name'=>'Suixi County','parent'=>5),
array('id'=>4,'name'=>'Changping','parent'=>7),
array('id'=>5,'name'=>'Huaibei','parent'=>1),
array('id'=>6,'name'=>'Chaoyang','parent'=>7),
array('id'=>7,'name'=>'Beijing','parent'=>0),
array('id'=>8,'name'=>'Shangdi','parent'=>2)
);

1. Recursion, search descendant tree

Copy code The code is as follows:

function subtree($arr,$id=0,$lev=1) {
$subs = array(); // Array of descendants
foreach($arr as $v) {
If($v['parent'] == $id) {
               $v['lev'] = $lev;
                  $subs[] = $v; // For example, find array('id'=>1,'name'=>'Anhui','parent'=>0),
                $subs = array_merge($subs,subtree($arr,$v['id'],$lev+1));
}
}
Return $subs;
}
$tree = subtree($area,0,1);
foreach($tree as $v) {
echo str_repeat(' ',$v['lev']),$v['name'],'
';
}

2. Recursion, find the family tree

Family tree applications, such as breadcrumb navigation Home page > Mobile phone type > CDMA mobile phone > Public Welfare PHP > Recursive application

Copy code The code is as follows:

function familytree($arr,$id) {
$tree = array();
foreach($arr as $v) {
           if($v['id'] == $id) {// Determine whether to find the parent column
If($v['parent'] > 0) { // parnet>0, it means there is a parent column
                    $tree = array_merge($tree,familytree($arr,$v['parent']));
            }
              $tree[] = $v; // Take finding Shangdi as an example
}
}
Return $tree;
}
print_r(familytree($area,8)); // Beijing->Haidian->Shangdi

2. Iterate and find the family tree

Copy code The code is as follows:

// Iteration is more efficient than recursion and does not require much code.
// Use iteration to find family tree recommendations
function tree($arr,$id) {
$tree = array();
While($id !== 0) {
         foreach($arr as $v) {
If($v['id'] == $id) {
                   $tree[] = $v;
                    $id = $v['parent'];
                 break;
                                                                       }
}
Return $tree;
}
print_r(tree($area,8));

http://www.bkjia.com/PHPjc/932494.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932494.htmlTechArticlephp implements unlimited classification, php implements level classification. Copy the code as follows: $area = array( array('id '=1,'name'='Anhui','parent'=0), array('id'=2,'name'='Haidian','parent'=7), array('i...
Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template