I want have two arrays
with date objects which I want to compare. The first array contains todays date and the next five days:
$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));
}
which gives the result:
0: "2018-09-14"
1: "2018-09-15"
2: "2018-09-16"
3: "2018-09-17"
4: "2018-09-18"
5: "2018-09-19"
the other array can contain multiple objects:
[{
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"
}]
Now I want to check if one of the days/dates of the first array will or is in the vacations_array. How can I achieve that?
EDIT
When a match is found, the second array(with the vacation-periods) where the match is true, must be assigned to a $vacation
-array
You can do it this way too:
$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);
use array_intersect()
Basically you do this within a loop for array 2:
$array_of_same_elements = array_intersect($array_1, $array_2[$i]['days']);
$array_of_same_elements will now contain the dates you are looking for.
A nice description of this is found here: https://www.w3schools.com/php/func_array_intersect.asp
it's a bit simple but it may fit your needs:
$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
";
}
}
return "no match found
";
}
echo checkEvents($event1['days'], $event2['days']);