首页 >后端开发 >php教程 >php递归无限极分门别类

php递归无限极分门别类

WBOY
WBOY原创
2016-06-13 11:56:36897浏览

php递归无限极分类

递归无限级分类有几种形式,我这里只举例比较常用的三种:

第一种:返回有排序的数组:

<?php $data = array(    1 => array(        'id' => 1,        'pid' => 0,        'user_name' => 'one',    ),    2 => array(        'id' => 2,        'pid' => 1,        'user_name' => 'two',    ),    3 => array(        'id' => 3,        'pid' => 1,        'user_name' => 'two',    ),    4 => array(        'id' => 4,        'pid' => 2,        'user_name' => 'three',    ),    5 => array(        'id' => 5,        'pid' => 2,        'user_name' => 'three',    ),);function genCate( $data, $pid = 0, $level = 0 ) {    $string = str_repeat( "--", $level ) . '|';    static $result = array();    $result = empty( $level ) ? array() : $result;    foreach ( $data as $k => $row ) {        if ( $row['pid'] == $pid ) {            $row['user_name'] = $string . $row['user_name'];            $result[] = $row['user_name'];            genCate( $data, $row['id'], $level + 1 );         }    }    return $result;}echo '<pre class="brush:php;toolbar:false">';$result = genCate( $data );print_r( $result );exit;?>


第二种:返回html字符串形式:


第三种:返回多维数组形式:


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn