其中:level 保存级别, next 保存下级的id, previons 保存上级的id, right 保存右邻的id
关于具体的数据组织视算法而定。
数的遍历算法,是很经典的了!
建议用数组计算,即一次性读入到数组,效率可能高一点
测试例,previons项未用
<?
$ar = array(
0,
array(id=> 1,id_u=>"001", level=>1, next=> 2, previons=> 0, right=> 5),
array(id=> 2,id_u=>"003", level=>2, next=> 0, previons=> 1, right=> 3),
array(id=> 3,id_u=>"004", level=>2, next=> 4, previons=> 1, right=> 0),
array(id=> 4,id_u=>"007", level=>3, next=> 0, previons=> 3, right=> 0),
array(id=> 5,id_u=>"002", level=>1, next=> 6, previons=> 0, right=>14),
array(id=> 6,id_u=>"004", level=>2, next=> 7, previons=> 5, right=>12),
array(id=> 7,id_u=>"003", level=>3, next=> 9, previons=> 6, right=> 8),
array(id=> 8,id_u=>"008", level=>3, next=> 0, previons=> 6, right=> 0),
array(id=> 9,id_u=>"005", level=>4, next=> 0, previons=> 7, right=>10),
array(id=>10,id_u=>"006", level=>4, next=> 0, previons=> 7, right=>11),
array(id=>11,id_u=>"007", level=>4, next=> 0, previons=> 7, right=> 0),
array(id=>12,id_u=>"006", level=>2, next=> 0, previons=> 5, right=>13),
array(id=>13,id_u=>"007", level=>2, next=> 0, previons=> 5, right=> 0),
array(id=>14,id_u=>"003", level=>1, next=>15, previons=> 0, right=> 0),
array(id=>15,id_u=>"007", level=>2, next=>16, previons=>14, right=>18),
array(id=>16,id_u=>"001", level=>3, next=> 0, previons=>15, right=>17),
array(id=>17,id_u=>"002", level=>3, next=> 0, previons=>15, right=> 0),
array(id=>18,id_u=>"008", level=>2, next=> 0, previons=>14, right=> 0)
);
//print_r($ar);
function tree($ar,$i,$key,$level) {
if($ar[$i][id_u] == $key (北联网教程,专业提供视频软件下载)
……