/**
* 递归
*/
public function getTree($data, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
{
$tree = array();
foreach($data as $k => $v) {
if($v[$pid] == $root) {
//父亲找到儿子
$v[$child] = $this->getTree($data, $pk, $pid, $child, $v[$pk]);
$tree[] = $v;
}
}
return $tree;
}
/**
* 迭代
*/
public function getTree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
{
$tree = array();
$packData = array();
foreach ($list as $data) {
$packData[$data[$pk]] = $data;
}
foreach ($packData as $key => $val) {
if ($val[$pid] == $root) {
//代表跟节点
$tree[] = &$packData[$key];
} else {
//找到其父类
$packData[$val[$pid]][$child][] = &$packData[$key];
}
}
return $tree;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。