我目前有两个数组,我想比较日期。这里是我的数组的结构:
$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('23-05-2014','24-05-2014','25-05-2014', '26-05-2014');
目的是检测$ bholidays数组中是否存在$ userdaysoff的值。
以上工作很好,检测到两个数组中都存在26-05-2014,但如果$ userdaysoff数组看起来像这样:
$userdaysoff = array('26-05-2014','27-05-2014','28-05-2014', '29-05-2014');
然后未检测到重复日期26-05-2014。
这有什么原因可以发生吗?
这是我如何运行我的代码:
$results = array_intersect($bholidays, $userdaysoff);
if($results){
foreach($results as $result){
echo 'yes';
}
} else {
echo 'no';
}
答案 0 :(得分:0)
你能不能简单地使用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.";
}
}
答案 1 :(得分: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';
}
运行此代码并检查它是否正常工作..
输出为是。