您的位置:首页 > 其它

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

2015-11-10 00:00 232 查看
$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来查询数据库并分页

$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天以上的算有效数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: