-
-
配列
- (
- [0] => 配列
- (
- [id] => 1
- [名前] => dev
- [親id] => 0
- [子] => 配列
- (
- [0] => 配列
- (
- [id] => 2
- [名前] => php
- [親id] => 1
- [子] => 配列
- (
- [0] => Array
- (
- [id] => 3
- [name] =>smarty
- [parentid] => 2
- )
[1] => 配列
- (
- [id] => 5
- [名前] => pdo
- [親 ID] => 2
- [子供] =>配列
- (
- [0] => 配列
- (
- [id] => 6
- [名前] => pdo-mysql
- [親id] => 5
- )
)
)
)
[1] =>配列
- (
- [id] => 7
- [name] => java
- [parentid] => 1
- )
)
)
[1] => Array
- (
- [id] => 4
- [name] => life
- [parentid] => 0
- )
- )
-
复制代网
看有存更简单の実現方法
-
-
$rows = array( - array(
- 'id' => 1,
- 'name' => 'dev',
- 'parentid' => ; 0
- ),
- array(
- 'id' => 2,
- 'name' => 'php',
- 'parentid' => 1
- ),
- array(
- 'id' => 3 ,
- 'name' => 'smarty',
- 'parentid' => 2
- ),
- array(
- 'id' => 'life',
- 'parentid' => 0
- ),
- array(
- 'id' => 5,
- 'name' => 'pdo',
- 'parentid' => 2
- ),
- array(
- 'id' => ; 6,
- 'name' => 'pdo-mysql',
- 'parentid' => 5
- ),
- array(
- 'id' => 'java',
- 'parentid' => 1
- )
- );
// 72648
- // 84072
- function findChild(&$arr,$id){
-
- $childs=array() ;
- foreach ($arr as $k => $v){
- if($v['parentid']== $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][' childs']=$rescurTree;
- }
- }
- return $childs;
- }
$tree=build_tree(0);
- echomemory_get_usage();
- print_r($tree);
- ?>
-
-
-
复制代
興味があるかもしれない記事:
php無制限分類例(タオバオ模倣品分類)
phpで実装した無限分類(再帰版)の例
PHP 配列を使用した無制限の分類 (データベースと再帰なし)
無制限の分類を再帰的に実装し、ドロップダウンリストを生成するためのPHPで書かれた関数
|