public function monthDiff2(){
// 导入一个店主记录
$info = M('member')->where(array('uid'=>UID))->find();
// 迭代法计算该店主的直接招商的所有IDs
$res = M('member')->field('uid,parent,nickname,sons')->select();
foreach($res as $key=>$vo){
// 方法一
$data[$vo['uid']]['id'] = $vo['uid'];
$data[$vo['uid']]['pid'] = $vo['parent'];
// 方法二
$parent[$vo['parent']][$vo['uid']] = $vo['uid'];
}
$func = 2;
$pid = 1;
import("@.Library.VMSExtends");
$obj = new \VMSExtends($host,$dbuser,$dbpwd,$dbname);
if($func==1){
$resTree = $obj->tree($data,$pid);
foreach($resTree as $key){
$list[$key['id']] = $key['id'];
asort($list);
}
}else{
$resTree = $obj->treeParent($parent,$pid);
asort($resTree);
$list = $resTree;
}
dump($list);
}class VMSExtends{
VMSExtends::$treeList = array(); 清空
static public $treeList = array();
public function __construct(){
}
static public $flg=0;
static public function tree(&$data,$pid = 0,$count=0) {
foreach ($data as $key => $value){
if($value['pid']==$pid){
$value['count'] = $count;
self::$treeList []=$value;
unset($data[$key]);
self::tree($data,$value['id'],$count+1);
}
//echo '['.self::$flg++.']';
}
return self::$treeList;
}
static public function treeParent(&$parent,$pid = 0,$count=0) {
if($parent[$pid]){
foreach ($parent[$pid] as $key => $value){
self::$treeList[$key] = $key;
if(empty($parent[$pid])){
unset($parent[$value]);
}
self::treeParent($parent,$key,$count+1);
}
unset($parent[$pid]);
}else{
self::$treeList[$pid] = $pid;
unset($parent[$pid]);
}
echo '['.self::$flg++.']';
return self::$treeList;
}
}条件:数据一共710条记录,本次查询的ID=1为最高级别(树的顶端)ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。