二维数组foreach嵌套遍历,判断连续3天以上的算有效数据

$studycourseinfo = $studycourseinfoModel->where($where)->limit($page->firstRow.','.$page->listRows)->order('create_time asc')->select();	
		//$studycourseinfos = $studycourseinfoModel->where($where)->order('course_id asc')->select();
		//$keys = array("user_id", "course_id");
		//$studycourseinfo_new = $this->makemultiarr($studycourseinfos,$keys);
		$cids = array();
		foreach($studycourseinfo as $key=>$value){
			$studycourseinfo_new[$value['user_id']][$value['course_id']][$value['create_time']] = $studycourseinfo[$key];
			$cids[$value['user_id'].'_'.$value['course_id']] = $value['user_id'].'_'.$value['course_id'];
		}
		$newStudyinfo = array();
		foreach($cids as $keys=>$values){
			$tempArr = explode('_', $values);
			//var_dump($tempArr);exit;
			$startTime = 0;
			$count = 0;
			$tempStudyinfo = array();
			foreach($studycourseinfo_new[$tempArr[0]][$tempArr[1]] as $k => $val){	
				if($startTime == 0){
					$startTime = $k;
					$count ++;
					$tempStudyinfo[] = $val;
					continue;
				}	
				if($k - $startTime == 86400){
					$startTime = $k;
					$count ++;
					$tempStudyinfo[] = $val;
				}else{
					//if($tempArr[1] == 11){
					//	echo($k.'</br>');
					//}
					if($count >= 3){
						//有效数据
						$newStudyinfo[] = $tempStudyinfo;
						$tempStudyinfo = array();
						$count = 0;
					}else{
						//无效丢弃
						$tempStudyinfo = array();
						$count = 0;
					}
					
					$startTime = $k;
					$count ++;
					$tempStudyinfo[] = $val;
				}
			}
			if($count >= 3){
				//有效数据
				$newStudyinfo[] = $tempStudyinfo;
				$tempStudyinfo = array();
				$count = 0;	
			}
		}
		//var_dump($newStudyinfo);
		
		$i = 0;
		foreach ($newStudyinfo as $_k => $_val) {
			foreach ($_val as $_kay => $_val) {
				$_newStudyinfo[$i] = $_val;
				$i++;
			}
		}
	
		//dump($_newStudyinfo);
		//dump($studycourseinfo_new);
		//exit;

数据查询结果

数据查询结果


转化成三维数组,用户id,课程id,详细信息

转化成三维数组,用户id,课程id,详细信息

因为查询数据库的结果被处理过,所以分页不准确,可 通过有效的二维数组,获取有效的$id,用$id来查询数据库并分页

$where['total_time'] = array("EGT", 30);
			
		/* $count				=	$studycourseinfoModel->where($where)->count();
		import('ORG.Util.Page');
		$page				=	new Page($count,10);
		$show				=	$page->show(); */
		
		//$studycourseinfo = $studycourseinfoModel->where($where)->limit($page->firstRow.','.$page->listRows)->order('create_time asc')->select();
		//先查询全部数据,下步筛选三维数字获取有效数据
		$studycourseinfo = $studycourseinfoModel->where($where)->order('create_time asc')->select();

		foreach($studycourseinfo as $key=>$value){
			$studycourseinfo_new[$value['user_id']][$value['create_time']] = $studycourseinfo[$key];
		}

		$newStudyinfo = array();
		foreach($studycourseinfo_new as $keys=>$values){
			$startTime = 0;
			$count = 0;
			$tempStudyinfo = array();
			foreach($values as $k => $val){	
				if($startTime == 0){
					$startTime = $k;
					$count ++;
					$tempStudyinfo[] = $val;
					continue;
				}	
				if($k - $startTime == 86400){
					$startTime = $k;
					$count ++;
					$tempStudyinfo[] = $val;
				}else{
					if($count >= 3){
						//有效数据
						$newStudyinfo[] = $tempStudyinfo;
						$tempStudyinfo = array();
						$count = 0;
					}else{
						//无效丢弃
						$tempStudyinfo = array();
						$count = 0;
					}
					
					$startTime = $k;
					$count ++;
					$tempStudyinfo[] = $val;
				}
			}
			if($count >= 3){
				//有效数据
				$newStudyinfo[] = $tempStudyinfo;
				$tempStudyinfo = array();
				$count = 0;	
			}
		}
		//转二维数组
		$i = 0;
		foreach ($newStudyinfo as $_k => $_val) {
			foreach ($_val as $_kay => $_val) {
				$_newStudyinfo[$i] = $_val;
				$i++;
			}
		}

		/* 
		 * 通过有效的二维数组,获取有效的$id,用$id来查询数据库并分页
		 */
		$id = array();
		foreach ($_newStudyinfo as $_ks => $_vals){
			$id[] = $_vals['id'];
		}
		$where['id'] = array('in',$id);
		
		$count				=	$studycourseinfoModel->where($where)->count();
		import('ORG.Util.Page');
		$page				=	new Page($count,20);
		$show				=	$page->show();
		$studycourseinfo_newss = $studycourseinfoModel->where($where)->limit($page->firstRow.','.$page->listRows)->order('create_time asc')->select();

转载请注明:PHP博客 » 二维数组foreach嵌套遍历,判断连续3天以上的算有效数据

转载于:https://my.oschina.net/guodingding/blog/528485

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值