0

I currently have 2 arrays where i would like to compare dates in. here are how my arrays are structured:

$bholidays = array('05-05-2014','26-05-2014');

$userdaysoff = array('23-05-2014','24-05-2014','25-05-2014', '26-05-2014');

The aim is to detect whether or not a value from $userdaysoff exists in the $bholidays array.

The above works great and detects that 26-05-2014 exists in both arrays, but if the $userdaysoff array looks like this:

$userdaysoff = array('26-05-2014','27-05-2014','28-05-2014', '29-05-2014');

Then the duplicate date 26-05-2014 is not detected.

Is there any reason why this would be occuring?

here is how i run my code:

$results = array_intersect($bholidays, $userdaysoff);
if($results){



foreach($results as $result){

echo 'yes';

}

} else {

echo 'no';  

}
danyo
  • 5,686
  • 20
  • 59
  • 119

2 Answers2

0

Could you not quite simply use in_array?

$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('23-05-2014','24-05-2014','25-05-2014', '26-05-2014');

$count = count($userdaysoff);
for($i = 0; $i == $count; $i++) {
    if(in_array($userdaysoff[$i], $bholidays)) {
        echo $userdaysoff[$i] . " is in array.";
    }
 }
Mark
  • 1,852
  • 3
  • 18
  • 31
0
    $bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('26-05-2014','27-05-2014','28-05-2014', '29-05-2014');

$results = array_intersect($bholidays, $userdaysoff);
if($results)
{
    foreach($results as $result)
    {
        echo 'yes';
    }
}
else
{
    echo 'no';
}

Run this code and check it works fine..

The output is yes.

Amit
  • 3,251
  • 3
  • 20
  • 31