当我使用For循环时,键传递下一步.而foreach数据没有改变数据.PHP



这是我的函数,用于计算两个人的交集时间范围。当我使用三个循环时,第一个循环可以将密钥传递到下一步,但他们使用的数据属于其他人的数据。我很困惑。

公共函数测试(({

//choose records of viva arrangement
$records = DB::table('application_student')
->where('allocationStatus', '=', 1)
->select('user_id', 'supervisor_id', 'assessor_id')
->get()->toArray();
//single records
foreach ($records as $key => $record) {
$viva[] = [
$sup_id = $record->supervisor_id, // key 0=>supervisor
$stu_id = $record->user_id,       // key 1=>student
];
}
// $x is the key number of viva[]
for ($x = 0; $x <count($records); $x++){
$sup_id = $viva[$x][0]; //supervisor_id of record $x
$stu_id = $viva[$x][1]; //student_id of record $x
//find availability of supervisor
$sup_time = DB::table('time_allocates')
->where('user_id','=',$sup_id)
->select('start','end')->get()->toArray();
//find availability of student
$stu_time = DB::table('time_allocates')
->where('user_id','=',$stu_id)
->select('start','end')->get()->toArray();
//every single availability of supervisor
foreach ($sup_time as $key => $supT) {
$sup[] = [
$s1 = strtotime($supT->start), //key 0=>start time
$e1 = strtotime($supT->end), //key 1=>end time
];
}
//every single availability of student
foreach ($stu_time as $key => $stuT) {
$stu[] = [
$s2 = strtotime($stuT->start), //key 0=>start time
$e2 = strtotime($stuT->end),   //key 1=>end time
];
}
//$i is the key number of single supervisor availability record
for ($i = 0; $i < count($sup_time); $i++) {
//$j is the key number of single student availability record
for ($j = 0; $j < count($stu_time); $j++) {
echo "$x+'$i'+'$j <br>";
$sup_s1 = $sup[$i][0]; // start time of supervisor
$sup_e1 = $sup[$i][1]; // end time of supervisor
$stu_s2 = $stu[$j][0]; // start time of student
$stu_e2 = $stu[$j][1]; // end time of student
//make sure the time range of student between supervisor has intersection
if($stu_s2>=$sup_s1 && $sup_e1>$stu_s2){
$start = $stu_s2;
if ($sup_e1<=$stu_e2){
$end = $sup_e1;
}else{
$end =$stu_e2;
}
if ($end-$start>=3600){
$start_date = date('Y-m-d H:i:s', $start);
$end_date = date('Y-m-d H:i:s', $end);
echo "$start_date '-' $end_date -$stu_id -$sup_id<br>";
}
}elseif ($sup_s1>=$stu_s2 && $sup_e1>$stu_s2){
$start =$sup_s1;
if ($sup_e1<=$stu_e2){
$end=$sup_e1;
}else{
$end=$stu_e2;
}
if ($end-$start>=3600){
$start_date = date('Y-m-d H:i:s', $start);
$end_date = date('Y-m-d H:i:s', $end);
echo "$start_date '-' $end_date -$stu_id-$sup_id <br>";
}
}
}
}
}
}

这是窗口的结果。学生id从2变为3,但数据始终使用2。请帮帮我,伙计们。在此处输入图像描述

您没有为每个循环重新初始化$sup

$sup[] = [
$s1 = strtotime($supT->start), //key 0=>start time
$e1 = strtotime($supT->end), //key 1=>end time
];

$stu[]相同对于第二个学生,它将在第一个学生的数据基础上添加数据。

你需要像这样做

//every single availability of supervisor
$sup = [];
foreach ($sup_time as $key => $supT) {
$sup[] = [
$s1 = strtotime($supT->start), //key 0=>start time
$e1 = strtotime($supT->end), //key 1=>end time
];
}
//every single availability of student
$stu = [];
foreach ($stu_time as $key => $stuT) {
$stu[] = [
$s2 = strtotime($stuT->start), //key 0=>start time
$e2 = strtotime($stuT->end),   //key 1=>end time
];
}

附言:你有很多多余的代码和许多额外的循环要删除。

最新更新