我有一个多维数组,数组深度未知,可能2维,可能4,5维乃至更多, 现在我得把这个数组按树形图显示出来, 必须用html的<ul>来实现 (因为还涉及到使用另一个jquery的插件)到底怎么写才能把这个数组变化成一个用<ul>写成的树形图啊?每个<li>里的内容是数组中的name字段即可.
Array
(
[0] => Array
(
[category_id] => 2
[parent_id] => 1
[name] => Bonnaviva
[is_active] => 1
[position] => 1
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 10
[parent_id] => 2
[name] => Tuotteet
[is_active] => 1
[position] => 1
[level] => 2
[children] => Array
(
[0] => Array
(
[category_id] => 4
[parent_id] => 10
[name] => Chillow - viilentävät tuotteet
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
) ) [1] => Array
(
[category_id] => 3
[parent_id] => 10
[name] => Climsom viilentävä/lämmittävä patjanpäällys
[is_active] => 1
[position] => 2
[level] => 3
[children] => Array
(
) ) [2] => Array
(
[category_id] => 5
[parent_id] => 10
[name] => Performance SleepWear -yöasut
[is_active] => 1
[position] => 3
[level] => 3
[children] => Array
(
) ) [3] => Array
(
[category_id] => 6
[parent_id] => 10
[name] => DreamSacks yöasut, vaatteet ja liinavaatteet
[is_active] => 1
[position] => 4
[level] => 3
[children] => Array
(
) ) [4] => Array
(
[category_id] => 7
[parent_id] => 10
[name] => Muut tuotteet
[is_active] => 1
[position] => 5
[level] => 3
[children] => Array
(
) ) ) ) ) ))
Array
(
[0] => Array
(
[category_id] => 2
[parent_id] => 1
[name] => Bonnaviva
[is_active] => 1
[position] => 1
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 10
[parent_id] => 2
[name] => Tuotteet
[is_active] => 1
[position] => 1
[level] => 2
[children] => Array
(
[0] => Array
(
[category_id] => 4
[parent_id] => 10
[name] => Chillow - viilentävät tuotteet
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
) ) [1] => Array
(
[category_id] => 3
[parent_id] => 10
[name] => Climsom viilentävä/lämmittävä patjanpäällys
[is_active] => 1
[position] => 2
[level] => 3
[children] => Array
(
) ) [2] => Array
(
[category_id] => 5
[parent_id] => 10
[name] => Performance SleepWear -yöasut
[is_active] => 1
[position] => 3
[level] => 3
[children] => Array
(
) ) [3] => Array
(
[category_id] => 6
[parent_id] => 10
[name] => DreamSacks yöasut, vaatteet ja liinavaatteet
[is_active] => 1
[position] => 4
[level] => 3
[children] => Array
(
) ) [4] => Array
(
[category_id] => 7
[parent_id] => 10
[name] => Muut tuotteet
[is_active] => 1
[position] => 5
[level] => 3
[children] => Array
(
) ) ) ) ) ))
赞成,通常递归解决这种问题稍微改一下就能满足你的要求了。。以下网页输出查源码看看
function getUL($array = array(), $deep = 0)
{
if(!empty($array))
{
foreach($array AS $v)
{
echo str_repeat("\t", $deep) . "{$v['name']}" . PHP_EOL;
if(!empty($v['children']))
{
getUL($v['children'], ++$deep);
}
}
}
}
Function DirTree($tid=0.11,$ParentDir=YJ_SysUpfilepath){
Global $File; $ParentDir=StrToLower($ParentDir);
$Tree=$File->getDir($ParentDir);
$_tid=$tid;
Foreach($Tree as $Dir){
$_tid+=0.01;
Echo "d.add({$_tid},".($tid*10).",'{$Dir[0]}','?work=list1&from=&cpath=".UrlEnCode($ParentDir.$Dir[0]."/")."','','FileList');\n";
DirTree(($_tid/10),$ParentDir.$Dir[0]."/");
}
}
$arr=Array
(
'0' => Array
(
'category_id' => 2,
'parent_id' => 1,
'name' => 'Bonnaviva',
'is_active' => 1,
'position' => 1,
'level' => 1,
'children' => Array
(
'0' => Array
(
'category_id' => 10,
'parent_id' => 2,
'name' => 'Tuotteet',
'is_active' => 1,
'position' => 1,
'level' => 2,
'children' => Array
(
'0' => Array
(
'category_id' => 4,
'parent_id' => 10,
'name' => 'Chillow - viilentävät tuotteet',
'is_active' => 1,
'position' => 1,
'level' => 3,
'children' => Array
(
) ), '1' => Array
(
'category_id' => 3,
'parent_id' => 10,
'name' => 'Climsom viilentävä/lämmittävä patjanpäällys',
'is_active' => 1,
'position' => 2,
'level' => 3,
'children' => Array
(
) ), '2' => Array
(
'category_id' => 5,
'parent_id' => 10,
'name' => 'Performance SleepWear -yöasut',
'is_active' => 1,
'position' => 3,
'level' => 3,
'children' => Array
(
) ), '3' => Array
(
'category_id' => 6,
'parent_id' => 10,
'name' => 'DreamSacks yöasut, vaatteet ja liinavaatteet',
'is_active' => 1,
'position' => 4,
'level' => 3,
'children' => Array
(
) ), '4' => Array
(
'category_id' => 7,
'parent_id' => 10,
'name' => 'Muut tuotteet',
'is_active' => 1,
'position' => 5,
'level' => 3,
'children' => Array
(
) ) ) ) ) ));
function tree($arr){
$return="<ul>";
if(is_array($arr)){
foreach($arr as $item){
$return.="<li>".$item[name];
if($item['children']){
$return.=tree($item['children']);
}
$return.="</li>\n";
}
} $return.="</ul>\n";
return $return;
}echo tree($arr);
看看这个行不行,如果不行,请自行修改