欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

看了版主的无限分类,分成树形结构后,如何还原

程序员文章站 2022-05-26 09:30:01
...
父子无限分类原始数组/*获取无线分类,对无线分类进行节点路径排列*/$a=array (  0 =>  array (    'id' => '134',    'fid' => '0',    'name' => 't',  ),  1 =>  array (    'id' => '133',    'fid' => '0',    'name' => 'k',  ),  2 =>  array (    'id' => '132',    'fid' => '0',    'name' => 'j',  ),  3 =>  array (    'id' => '137',    'fid' => '133',    'name' => '我是k的分类1',  ),  4 =>  array (    'id' => '136',    'fid' => '134',    'name' => '我是t的分类t2',  ),  5 =>  array (    'id' => '135',    'fid' => '134',    'name' => '我是t的分类t1',  ),  6 =>  array (    'id' => '138',    'fid' => '137',    'name' => '我是k的分类1-1',  ),  7 =>  array (    'id' => '139',    'fid' => '138',    'name' => '我是k的分类1-1-1',  ),  8 =>  array (    'id' => '140',    'fid' => '139',    'name' => '我是k的分类1-1-1-1',  ),)?>开始形成树,并且增加路径,树形数组为$treesArray(    [0] => Array        (            [id] => 134            [fid] => 0            [name] => t            [path] => 134            [_sub] => Array                (                    [0] => Array                        (                            [id] => 136                            [fid] => 134                            [name] => 我是t的分类t2                            [path] => 134,136                        )                    [1] => Array                        (                            [id] => 135                            [fid] => 134                            [name] => 我是t的分类t1                            [path] => 134,135                        )                )        )    [1] => Array        (            [id] => 133            [fid] => 0            [name] => k            [path] => 133            [_sub] => Array                (                    [0] => Array                        (                            [id] => 137                            [fid] => 133                            [name] => 我是k的分类1                            [path] => 133,137                            [_sub] => Array                                (                                    [0] => Array                                        (                                            [id] => 138                                            [fid] => 137                                            [name] => 我是k的分类1-1                                            [path] => 133,137,138                                            [_sub] => Array                                                (                                                    [0] => Array                                                        (                                                            [id] => 139                                                            [fid] => 138                                                            [name] => 我是k的分类1-1-1                                                            [path] => 133,137,138,139                                                            [_sub] => Array                                                                (                                                                    [0] => Array                                                                        (                                                                            [id] => 140                                                                            [fid] => 139                                                                            [name] => 我是k的分类1-1-1-1                                                                            [path] => 133,137,138,139,140                                                                        )                                                                )                                                        )                                                )                                        )                                )                        )                )        )    [2] => Array        (            [id] => 132            [fid] => 0            [name] => j            [path] => 132        ))但是我有个问题,如何还原树呢就是还原成原始数组那种二维形式呢,然后多个path值,因为刚才最主要的目的是加个path值$a=array (  0 =>  array (    'id' => '134',    'fid' => '0',    'name' => 't',    'path'=>'134'  ),  1 =>  array (    'id' => '133',    'fid' => '0',    'name' => 'k',    'path'=>' 133,137'  ),.........................我只想用递归可惜才学疏浅,一直不会递归,只能取道一层。。。是在写不出来function test($array) {	foreach ($array as $k=>$v) {			if (array_key_exists('_sub', $v)) {				$temp[]=array_slice($v, 0,4);				 //$temp[]=test($v[''_sub'']);			} else {				$temp[]=$v;			}	}	return $temp;}$x=test($trees);print_r($x);打印出来的数组只有3个Array(    [0] => Array        (            [id] => 134            [fid] => 0            [name] => t            [path] => 134        )    [1] => Array        (            [id] => 133            [fid] => 0            [name] => k            [path] => 133        )    [2] => Array        (            [id] => 132            [fid] => 0            [name] => j            [path] => 132        ))。。。。真心求代码


回复讨论(解决方案)

 Array        (            'id' => 134,            'fid' => 0,            'name' => 't',            'path' => 134,            '_sub' => Array                (                    0 => Array                        (                            'id' => 136,                            'fid' => 134,                            'name' => '我是t的分类t2',                            'path' => '134,136'                        ),                     1 => Array                        (                            'id' => 135,                            'fid' => 134,                            'name' => '我是t的分类t1',                            'path' => '134,135'                        )                 )         ),     1 => Array        (            'id' => 133,            'fid' => 0,            'name' => 'k',            'path' => 133,            '_sub' => Array                (                    0 => Array                        (                            'id' => 137,                            'fid' => 133,                            'name' => '我是k的分类1',                            'path' => '133,137',                            '_sub' => Array                                (                                    0 => Array                                        (                                            'id' => 138,                                            'fid' => 137,                                            'name' => '我是k的分类1-1',                                            'path' => '133,137,138',                                            '_sub' => Array                                                (                                                    0 => Array                                                        (                                                            'id' => 139,                                                            'fid' => 138,                                                            'name' => '我是k的分类1-1-1',                                                            'path' => '133,137,138,139',                                                            '_sub' => Array                                                                (                                                                    0 => Array                                                                        (                                                                            'id' => 140,                                                                            'fid' => 139,                                                                            'name' => '我是k的分类1-1-1-1',                                                                            'path' => '133,137,138,139,140'                                                                        )                                                                 )                                                         )                                                 )                                         )                                 )                         )                 )         ),     2 => Array        (            'id' => 132,            'fid' => 0,            'name' => 'j',            'path' => 132        ) );$temp = array();function test($array,&$temp) { 	    foreach ($array as $k=>$v) {            if (array_key_exists('_sub', $v)) {                 $temp[]=array_slice($v, 0,4);                test($v['_sub'],$temp);             } else { 				array_push($temp, $v);             }    } } test($arr,$temp);print_r($temp);?>

你把函数修改为

function find_child($ar) {  foreach($ar as $v) $t[$v['id']] = $v;  foreach ($t as $k => $item){    $t[$k]['path'] = $item['id'];    if( $item['fid'] ) {      $t[$item['fid']]['_sub'][$item['id']] =& $t[$k];      $t[$k]['path'] = $t[$item['fid']]['path'] . ',' . $t[$k]['id'];    }  }  return $t;}
$a=array (  0 =>  array (    'id' => '134',    'fid' => '0',    'name' => 't',  ),  1 =>  array (    'id' => '133',    'fid' => '0',    'name' => 'k',  ),  2 =>  array (    'id' => '132',    'fid' => '0',    'name' => 'j',  ),  3 =>  array (    'id' => '137',    'fid' => '133',    'name' => '我是k的分类1',  ),  4 =>  array (    'id' => '136',    'fid' => '134',    'name' => '我是t的分类t2',  ),  5 =>  array (    'id' => '135',    'fid' => '134',    'name' => '我是t的分类t1',  ),  6 =>  array (    'id' => '138',    'fid' => '137',    'name' => '我是k的分类1-1',  ),  7 =>  array (    'id' => '139',    'fid' => '138',    'name' => '我是k的分类1-1-1',  ),  8 =>  array (    'id' => '140',    'fid' => '139',    'name' => '我是k的分类1-1-1-1',  ),);$r = find_child($a);foreach($r as $k=>$v) if(isset($v['_sub'])) unset($r[$k]['_sub']);print_r($r);
Array(    [134] => Array        (            [id] => 134            [fid] => 0            [name] => t            [path] => 134        )    [133] => Array        (            [id] => 133            [fid] => 0            [name] => k            [path] => 133        )    [132] => Array        (            [id] => 132            [fid] => 0            [name] => j            [path] => 132        )    [137] => Array        (            [id] => 137            [fid] => 133            [name] => 我是k的分类1            [path] => 133,137        )    [136] => Array        (            [id] => 136            [fid] => 134            [name] => 我是t的分类t2            [path] => 134,136        )    [135] => Array        (            [id] => 135            [fid] => 134            [name] => 我是t的分类t1            [path] => 134,135        )    [138] => Array        (            [id] => 138            [fid] => 137            [name] => 我是k的分类1-1            [path] => 133,137,138        )    [139] => Array        (            [id] => 139            [fid] => 138            [name] => 我是k的分类1-1-1            [path] => 133,137,138,139        )    [140] => Array        (            [id] => 140            [fid] => 139            [name] => 我是k的分类1-1-1-1            [path] => 133,137,138,139,140        ))

代码我先收下了,刚才看了一遍没有看懂。。。。。。



谢谢两位,再次谢过了,分有点少请不要介意


哎 什么时候我能像2位那么强啊