参考网址
http://www.qunfei.com/htmls/webdevelop/php/2012/0116/819.html
function dafenglei_arr($m,$id)
{
global $class_arr;
global $ppid;
if($id=="") $id=0;
$n = str_pad('',$m,'-',STR_PAD_RIGHT);
$n = str_replace("-"," ",$n);
for($i=0;$i<count($class_arr);$i++){
if($class_arr[$i]['classFID']==$id){
echo "<tr>\n";
echo "<td height=\"28\" class=\"font\">".$n."|--<a href=\"?action=edit&id=".$class_arr[$i]['classid']."\">".$class_arr[$i]['className'].$m."</a></td>\n";
echo "</tr>\n";
dafenglei_arr($m+1,$class_arr[$i]['classid']);
}
}
}
输出的格式为
<tr>
<td> 栏目1</td>
<td> 栏目1-1</td>
<td> 栏目1-2</td>
<td> 栏目1-3</td>
<td> 栏目1-3-1</td>
</tr>
现在想转换成
<ul id="files">
<li id="m33"><a href="javascript:void(0);" >栏目一</a>
<ul>
<li><a href="/news-p11-pp33-ppp140" ><span class="">栏目1-1</span></a></li>
<li><a href="/news-p11-pp33-ppp141" ><span class="">栏目1-2</span></a></li>
<li><a href="javascript:void(0);" >栏目1-3</a>
<ul>
<li><a href="/news-p11-pp33-ppp140" ><span class="">栏目1-3-1</span></a></li>
</ul>
</li>
</ul></li>
<li id="m34">...........等类似</li>
</ul>
------解决方案--------------------
$ar = array (<ul><li>栏目一<ul><li>栏目1-1<ul><li>栏目1-1-1</li></ul></li></ul>
2 =>
array (
'classid' => '6',
'classfid' => '0',
'classname' => '栏目一',
),
1 =>
array (
'id' => '7',
'classfid' => '6',
'classname' => '栏目1-1',
),
0 =>
array (
'classid' => '10',
'classfid' => '7',
'classname' => '栏目1-1-1',
),
);
echo '<ul>';
foreach($ar as $item) {
if($item['classfid'] == 0) $deep = 0;
$t = substr_count($item['classname'], '-');
if($t < $deep) echo '</li></ul>';
if($t > $deep) echo '<ul>';
echo '<li>'.$item['classname'];
$deep = $t;
}
while($deep--) echo '</li></ul>';