我这里分享一个递归的。运行很慢。数据量一大。服务器的压力会很大。
public function findChild(&$arr, $id)
{
$chlids = array();
foreach ($arr as $key => $vel) {
if ($vel['pid'] == $id) {
$chlids[] = $vel;
}
}
return $chlids;
} public function tree($id)
{
$m_stags = M('admin_stags');
$sel = $m_stags->order('id asc')->select();
$chlids = $this->findChild($sel, $id);//查找子级
if (empty($chlids)) {
return null;
}
foreach ($chlids as $key => $vel) {
$rescurTree = $this->tree($vel['id']);//查找子级是否存在子级
if ($rescurTree != null) {
$chlids[$key]['child'] = $rescurTree;
}
}
return $chlids;
}
这个方法虽然可以得到想要的结果但是页面很慢。分类超过10层进入页面耗时14.5秒。求优化。求极致。
public function findChild(&$arr, $id)
{
$chlids = array();
foreach ($arr as $key => $vel) {
if ($vel['pid'] == $id) {
$chlids[] = $vel;
}
}
return $chlids;
} public function tree($id)
{
$m_stags = M('admin_stags');
$sel = $m_stags->order('id asc')->select();
$chlids = $this->findChild($sel, $id);//查找子级
if (empty($chlids)) {
return null;
}
foreach ($chlids as $key => $vel) {
$rescurTree = $this->tree($vel['id']);//查找子级是否存在子级
if ($rescurTree != null) {
$chlids[$key]['child'] = $rescurTree;
}
}
return $chlids;
}
这个方法虽然可以得到想要的结果但是页面很慢。分类超过10层进入页面耗时14.5秒。求优化。求极致。
2、您说 分类超过10层进入页面耗时14.5秒 那么请问你总体的数据规模有多大,是否建有关键的索引
3、高效的检索方法是采用 预排序的左右值算法(插入、摘枝 要多耗费点时间)你是在使用 TP 吧?他的 relation 可以让你节省不少时间和空间