我有以下数组:
$dates[] = array(
'Sunday' => '13 Jul 2014',
'Monday' => '14 Jul 2014',
'Tuesday' => '15 Jul 2014',
);
我想搜索数组以查看今天是否匹配。我今天将其设置为变量如下:
$today = date('d M Y', strtotime('today'));
我正在尝试使用简单的foreach搜索匹配:
foreach ($dates as $day => $date) {
if ($today == $date) {
echo 'match';
} else {
echo 'no match';
}
}
但是,这总是会返回“不匹配”。我有什么想法吗?
此处的实时版本:http://viper-7.com/4INcaz
答案 0 :(得分:4)
这是你的阵列:
array(
0 => array(
'Sunday' => '13 Jul 2014',
'Monday' => '14 Jul 2014',
'Tuesday' => '15 Jul 2014',
)
);
因为您已将数组分配给$dates[]
。你应该这样做:
foreach ($dates[0] as $day => $date) {
if ($today == $date) {
echo 'match';
} else {
echo 'no match';
}
}
或者这个:
$dates = array(
'Sunday' => '13 Jul 2014',
'Monday' => '14 Jul 2014',
'Tuesday' => '15 Jul 2014',
);