求一个高效的运行快的无限查找方法。递归的很慢。求大腿
我这里分享一个递归的。运行很慢。数据量一大。服务器的压力会很大。
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秒。求优化。求极致。