php 递归 无限级分类并返回数组的例子,有需发的朋友可以参考下。
<?php
/**
* 递归 无限级分类 返回数组
* link:www.jb200.com
* date:2013/2/21
*/
$conn = mysql_connect('localhost','root','123456');
mysql_select_db('test');
mysql_query("set names 'utf8'");
function getCate($pid = 0)
{
$sql = "select * from cate where pid=".$pid;
$res = mysql_query($sql);
if($res)
{
while($row = mysql_fetch_assoc($res)){
$categories[] = $row;
}
}
if(0 < count($categories))
{
for($i = 0; $i < count($categories); $i++)
{
$categories[$i]['child'] = getCate($categories[$i]['id']);
}
}
return $categories;
}
//调用
$cate = getCate(0);
?>