写了一个简单的demo 供大家参考下:
index.php
header("Content-type: text/html; charset=utf-8");
/**
* 无限极分类
*/
$array = array(
1 => array("id" => 1, "name" => "食物", "pid" => 0),
2 => array("id" => 2, "name" => "衣服", "pid" => 0),
3 => array("id" => 3, "name" => "水果", "pid" => 1),
4 => array("id" => 4, "name" => "蔬菜", "pid" => 1),
5 => array("id" => 5, "name" => "男装", "pid" => 2),
6 => array("id" => 6, "name" => "女装", "pid" => 2),
7 => array("id" => 7, "name" => "苹果", "pid" => 3),
8 => array("id" => 8, "name" => "葡萄", "pid" => 3),
9 => array("id" => 9, "name" => "番茄", "pid" => 4),
10 => array("id" => 10, "name" => "土豆", "pid" => 4),
11 => array("id" => 11, "name" => "T恤", "pid" => 5),
12 => array("id" => 12, "name" => "衬衫", "pid" => 5),
13 => array("id" => 13, "name" => "连衣裙", "pid" => 6),
14 => array("id" => 14, "name" => "短裤", "pid" => 6),
);
// 1 递归实现
function getList ($array, $pid, $tmp = "")
{
for ($i = 1; $i <= count($array); $i++) {
if ($array[$i]['pid'] == $pid) {
echo $tmp . $array[$i]['name'] . "<br/>";
getList($array, $array[$i]['id'], $tmp . "-----");
}
}
}
getList($array, 0);
访问index.php