我想要两个带有日期对象的arrays
要比较。第一个数组包含今天的日期和接下来的五天:
$today = date('Y-m-d');
$fiveDays = [];
for($i=0; $i <= 5; $i++){
$today = date('Y-m-d', strtotime('+1 day', strtotime($today)));
$fiveDays[] = date('Y-m-d', strtotime($today));
}
给出结果:
0: "2018-09-14"
1: "2018-09-15"
2: "2018-09-16"
3: "2018-09-17"
4: "2018-09-18"
5: "2018-09-19"
另一个数组可以包含多个对象:
[{
days: (4) ["2018-09-13", "2018-09-14", "2018-09-15", "2018-09-16"]
duration: 4
end: "2018-09-16"
name: "vacation blabla"
start: "2018-09-13"
},
{
days: (5) ["2018-09-20", "2018-09-21", "2018-09-22", "2018-09-23", "2018-09-24"]
duration: 5
end: "2018-09-24"
name: "vacation blabla"
start: "2018-09-20"
}]
现在,我要检查第一个数组的日期/日期中的某一个是否将在Vacations_array中。我该如何实现?
编辑
找到匹配项后,必须将匹配项为true的第二个数组(带有休假期)分配给$vacation
数组
答案 0 :(得分:2)
使用array_intersect()
基本上,您可以在数组2的循环内执行此操作:
$array_of_same_elements = array_intersect($array_1, $array_2[$i]['days']);
$ array_of_same_elements现在将包含您要查找的日期。
对此有一个很好的描述: https://www.w3schools.com/php/func_array_intersect.asp
答案 1 :(得分:1)
您也可以这样操作:
$result_array = array();
$found = false;
for($i=0; $i<count($vacation_array); $i++) {
$found = false;
for($j=0; $j<count($vacation_array[$i]['days']); $j++) {
if(in_array($vacation_array[$i]['days'][$j], $array_1) && !$found) {
$result_array[] = $vacation_array[$i];
$found = true;
}
}
}
print_r($result_array);
答案 2 :(得分:0)
这有点简单,但它可能满足您的需求:
$event1 = ['days'=>["2018-09-13", "2018-09-14", "2018-09-15", "2018-09-16"]];
$event2 = ['days'=>["2018-09-20", "2018-09-21", "2018-09-22", "2018-09-23", "2018-09-24"]];
function checkEvents($ev1, $ev2){
foreach($ev1 AS $day){
if(in_array($day, $ev2)){
return $day." is found in both events\n";
}
}
return "no match found\n";
}
echo checkEvents($event1['days'], $event2['days']);