我一直试图将这个数组合并几个小时,但是我没有得到它,我有两个数组,一个叫''期',另一个叫'时间'
期
"periodo":{"periodo":[["2017-06-22","2017-06-23","2017-06-24"],["2017-06-22","2017-06-23","2017-06-24"]]}
蒂恩波
"tiempo":[{"inicio":"2:00 am","fin":"3:00 am"},{"inicio":"3:00 am","fin":"4:00 am"},{"inicio":"4:00 am","fin":"5:00 am"},{"inicio":"5:00 am","fin":"6:00 am"},{"inicio":"6:00 am","fin":"7:00 am"},{"inicio":"2:00 am","fin":"3:00 am"},{"inicio":"3:00 am","fin":"4:00 am"},{"inicio":"4:00 am","fin":"5:00 am"},{"inicio":"5:00 am","fin":"6:00 am"},{"inicio":"6:00 am","fin":"7:00 am"}]
这是我的代码
public function testArray(Request $request)
{
if ($request!="" && $request->idUser!="")
{
$timeslot= new Timeslot;
$idUser=$request->idUser;
$fecha1=$request->fecha1;
$fecha2=$request->fecha2;
$tiempo1=$request->desde;
$tiempo2=$request->hasta;
$slotD=$request->slotH;
$i=0;
$k=0;
$j=0;
$l=0;
$m=0;
foreach ($fecha1 as $key => $value) {
$date1[$k]=date('Y-m-d', strtotime($value));
$k++;
}
foreach ($fecha2 as $key => $value) {
$date2[$i]=date('Y-m-d', strtotime($value));
$i++;
}
foreach ($tiempo1 as $key => $value) {
$value=strtotime($value);
$time1[$l]=$value;
$l++;
}
foreach ($tiempo2 as $key => $value) {
$value=strtotime($value);
$time2[$m]=$value;
$m++;
}
foreach ($slotD as $key => $value) {
$value=str_replace("minutos","minutes",$value);
$value=str_replace("horas","hours",$value);
$value=str_replace("hora","hours",$value);
$slot[$j]=$value;
$j++;
}
$array=array("horaInicio"=>$time1,"Horafin"=>$time2);
for($l1=0;$l1<count($date1);$l1++)
{
//$datePeriod["periodo"]=$timeslot->returnDates($date1[$l],$date2[$l]);
$datePeriod["periodo"][$l1]=$timeslot->createDateRangeArray($date1[$l1],$date2[$l1]);
//echo $datePeriod[$l]->format('Y-m-d');
}
$n=0;
for($i2=0;$i2<count($time1);$i2++)
{
while ($time1[$i2] < $time2[$i2]) {
//echo date('g:i:s', $time) . " - ";
$tiempo[$n]["inicio"]=date('g:i a', $time1[$i2]);
$time1[$i2] = strtotime('+'.$slot[$i2], $time1[$i2]);
//echo date('g:i a', $time) . "<br>";
$tiempo[$n]["fin"]=date('g:i a', $time1[$i2]);
$n++;
}
}
foreach ($datePeriod as $key => $value)
{
$id=rand();
foreach ($tiempo as $value2) {
/*
$timeslot= new Timeslot;
$timeslot->user_id=$idUser;
$timeslot->slot_date=$value[$key];
$timeslot->inicio=$value2['inicio'];
$timeslot->fin=$value2['fin'];
$timeslot->slot_id=$id;
$timeslot->slot_status=0;
$timeslot->save();*/
//$response["fecha"][]=$value["periodo"][$key];
$response["fecha"][]=$value2['inicio'];
$response["fecha"][]=$value2['fin'];
$response["fecha"][]=$id;
}
}
}else{
$response["success"]=false;
}
// print_r($tiempo);
//print_r($datePeriod);
return array("periodo"=>$dates,"tiempo"=>$tiempo);
// return $response;
}
这是函数createDateRangeArray()
的代码 function createDateRangeArray($strDateFrom,$strDateTo)
{
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom)
{
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
return $aryRange;
}
我希望我的阵列看起来像这样
"fecha":["2017-06-22","2:00 am","3:00 am",11305,"2017-06-22","3:00 am","4:00 am",11305,"2017-06-22","4:00 am","5:00 am",11305,"2017-06-22","5:00 am","6:00 am",11305,"2017-06-22","6:00 am","7:00 am",11305,"2017-06-23","2:00 am","3:00 am",31662,"2017-06-23","3:00 am","4:00 am",31662,"2017-06-23","4:00 am","5:00 am",31662,"2017-06-23","5:00 am","6:00 am",31662,"2017-06-23","6:00 am","7:00 am",31662,"2017-06-24","2:00 am","3:00 am",23887,"2017-06-24","3:00 am","4:00 am",23887,"2017-06-24","4:00 am","5:00 am",23887,"2017-06-24","5:00 am","6:00 am",23887,"2017-06-24","6:00 am","7:00 am",23887]
我想到的方法之一是根据数组中元素的数量来使foreach迭代,但是我仍然无法做到这一点,我以前用这种类型的数组尝试了类似的东西
阵列
:
[["2017-06-22"],{"1":"2017-06-23"},{"2":"2017-06-24"}]
码
foreach ($dates as $key => $value)
{
$id=rand();
foreach ($tiempo as $value2) {
/* $timeslot= new Timeslot;
$timeslot->user_id=$idUser;
$timeslot->slot_date=$value[$key];
$timeslot->inicio=$value2['inicio'];
$timeslot->fin=$value2['fin'];
$timeslot->slot_id=$id;
$timeslot->slot_status=0;
$timeslot->save();
*/
$response["fecha"][]=$value[$key];
$response["fecha"][]=$value2['inicio'];
$response["fecha"][]=$value2['fin'];
$response["fecha"][]=$id;
}
}
但是,我找不到访问数组内部值的方法,因为每个数组都会有所不同,我怎么能解决这个问题?