Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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';  

}
share|improve this question
    
Please run your code carefully. It runs fine. –  Rolen Koh Mar 19 '14 at 10:23
    
Yes it does works correctly... –  amit Mar 19 '14 at 10:25
    
and whats the output u get?? –  amit Mar 19 '14 at 10:32
    
if i structure the array like my top example it works and $results returns a result of yes, the second example returns no –  danyo Mar 19 '14 at 10:33
    
Check my updated answer. –  amit Mar 19 '14 at 10:36

3 Answers 3

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.";
    }
 }
share|improve this answer
    $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.

share|improve this answer
    
this is my exact code i have posted in my question? –  danyo Mar 19 '14 at 10:38
    
It is working fine.. Thats what iam trying to tell u... –  amit Mar 19 '14 at 10:40
    
yes, but it isnt for me –  danyo Mar 19 '14 at 10:43
    
how can it not? what is the php version? –  amit Mar 19 '14 at 10:44

@danyo, seems you have copied pasted string and it has some extra bytes otherwise your code is correct and working fine.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.