树形结构

树形结构

针对树级结构,常用的策略就是递归,另外一种,就是组装成字段。然后从字段里面读字段,这样可以减少递归。

如下,对树级的data进行遍历,但是开销略大。

public static function setRemark(&$data,$pid,$remark){
		foreach($data as &$row){
			if(!isset($row->parentid)) continue;
			if($row->parentid == $pid){
				$row->remark=$remark.'>'.$row->name;//设置remark  分割 >
				self::setRemark($data,$row->id,$row->remark);
			}
		}
	}

例外一种,就是想将数组保存成map,或者关联数组,如下代码:

var cityList =[];//_city中的对象
	var cityCode =item.substring(0,6);
	var cur  = _city[cityCode];
	cityList.push(cur);
	while(cur.pid!=='0'){
		//前进一步
		cur=_city[cur.pid];
		cityList.push(cur);
	}
	//取反顺序,即由根节点往下
	cityList.reverse();
	city= cityList.map(x=>x.name).join(' ');

php的代码示例

<?php
//机构管理

class StatisticsUtils{
	public static $organizationData;
	public static $organizationPath;
	
	/**********	
		2018/7/23
		显示机构列表名称
	*********/
	public static function hospital($params){
		$db = Zend_Registry::get('db'); 
		$structureId = $params['structure_id'];	//structure_id
		$searchDateKey = isset($params['searchDateKey'])?$params['searchDateKey']:1;
		$startdate =  date("Y-m-d 00:00:00",strtotime($params['start_date'])); 
		$stopdate =  date("Y-m-d 23:59:59",strtotime($params['stop_date']));		

		//时间
		$where = "";
		if($searchDateKey==1){//预约日期
			$where=" and ( c.conversation_schedule_date between '$startdate' and '$stopdate' )";
		}else{//下单日期
			$where=" and ( ct.pay_time between '$startdate' and '$stopdate' )";
		}

		//查询内容  "专家医生"; 
		if(isset($params['doctor'])){
			$schedule_ids =$db->fetchCol('select schedule_id from schedule where user_id =:id',array('id'=>$params['doctor']));	
			if(count($schedule_ids)){//查找到医生的排班
				$where .= $db->quoteInto('and ct.schedule_id IN(?)', $schedule_ids);
			}else{//否则直接返回空结果集
				return array();
			}
		}

		$sql= "SELECT ct.organization_id,conversation_schedule_date,pay_price  FROM conversation_treatment AS ct  ".
				"LEFT JOIN conversation AS c ON ct.conversation_id = c.conversation_id  ".
				"WHERE  ".
				"	ct.treatment_type = 1  ". //门诊
				"AND c.conversation_type = 2  ". //医疗
				"AND c.structure_id = $structureId   ".//当前机构
				" $where ORDER BY ct.conversation_id ASC  ";
		// echo $sql;exit;
		$res = $db->query($sql);
		
		$rows = $res->fetchAll();
		
		//对数据进行统计分析
		$groupM = self::getOrganization($structureId);
		$data = array();  //data ['organization_id']['date'][]
		foreach($rows as $k=>$row){
			$oid =$groupM[$row['organization_id']];
			$date = $row['conversation_schedule_date'];
			if(!isset($data[$oid])){
				$data[$oid]=array();
			}
			if(!isset($data[$oid][$date])){
				$data[$oid][$date]=array('price'=>0,'count'=>0);
			}
			//统计
			$data[$oid][$date]['price']+=$row['pay_price'];
			$data[$oid][$date]['count']+=1;
		}
		
		$result =array();
		foreach($data as $oid=>$oidArr){
			foreach($oidArr as $date=>$dateArr){
				$basehospital = self::$organizationData[$oid];
				// {'date':'2019-07-01','basehospital':'华山医院','price':'11','count':'1'},
				$result[]=array('date'=>$date,'basehospital'=>$basehospital,'price'=>$dateArr['price'],'count'=>$dateArr['count']);
			}
		}
		//排序
		function cmp($a,$b){
			if($a['date']==$b['date']){
				return 0;
			}
			return date($a['date'])>date($b['date'])?1:-1;
		}
		usort($result,'cmp');
		return $result;
	}
	/****************
	 * 2019年7月5日
	 * 返回每个组织所归属的医院的map
	 ****************/
	public static function getOrganization($structureId){
		$db = Zend_Registry::get('db'); 
		$rows=$db->fetchAll('select organization_id id ,organization_parent_id pid,organization_name name from  organization where structure_id =:id;',array('id'=>$structureId));
		self::$organizationData = array_column($rows,'name','id');
		$mapping = array_column($rows,'pid','id');

		// echo json_encode($mapping );exit;
		$result=array();
		foreach($mapping as $id=>$pid){
			$cur=$pid;
			$path=array($pid);
			while($cur>0){
				// var_dump($cur);
				$cur=$mapping[$cur];
				$path[]=$cur;
				if(count($path)>20){
					die("maximum recursion depth exceeded ");
				}
			}
			$result[$id]=join(',',$path);
		}
		self::$organizationPath = $result;
		$data = array();
		foreach($result as $k=>$v){
			$tmp = explode(',',$v);
			$len = count($tmp);
			if($len>1){
				$data[$k]=$tmp[$len-2];
			}
		}
		return $data;
	}
	/****************
	 * 2019年7月8日
	 * 获取医院
	 ****************/
	public static function getHospital($params){
		$db = Zend_Registry::get('db'); 
		$rows=$db->fetchAll('select organization_id value ,organization_name name from  organization where structure_id =:id and organization_parent_id = 0;',array('id'=>$params['structure_id']));
		$option =  isset($params['option']) ? '请选择机构':'全部机构';
		return array_merge(array(array('name'=>$option,'value'=>-1)),$rows);
	}
	/************
	 * 2019年7月8日
	 * 获取指定医院的医生
	 ************/
	public static function getDoctor($params){
		$db = Zend_Registry::get('db'); 
		$structureId = $params['structure_id'];
		$id  = $params['hospital_id'];

		$org = self::getOrganization($structureId);
		$orgIds=array();
		foreach($org as $k=>$val){
			if($val == $id){
				$orgIds[]=$k;
			}
		}
		$orgIds[]=$id;
		$where = $db->quoteInto('organization_id in (?)',$orgIds);
		$rows=$db->fetchAll("select user.user_id,user_name from user left join user_extend on user.user_id = user_extend.user_id where $where");
		return $rows;
	}
	
}