简体   繁体   English

php array_intersect无法正常运行

[英]php array_intersect not functioning correctly

I currently have 2 arrays where i would like to compare dates in. here are how my arrays are structured: 我目前有2个要比较日期的数组。这是数组的结构:

$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. 目的是要检测$ bholidays数组中是否存在来自$ userdaysoff的值。

The above works great and detects that 26-05-2014 exists in both arrays, but if the $userdaysoff array looks like this: 上面的方法效果很好,并检测到两个数组中都存在26-05-2014,但是如果$ userdaysoff数组如下所示:

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

Then the duplicate date 26-05-2014 is not detected. 然后,不会检测到重复的日期26-05-2014。

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

}

Could you not quite simply use in_array? 您能不能简单地使用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.";
    }
 }
    $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. 输出是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM