我有这个代码:
$specific_discount_dates = get_field('specific_discount_dates', 'options');
$specific_discount_dates_array = array();
if (have_rows('specific_discount_dates', 'options')) {
while ( have_rows( 'specific_discount_dates', 'options') ) : the_row();
$other_discount_dates = get_sub_field('other_discount_date');
$date_obj = date_create_from_format('d/m/Y', $other_discount_dates);
$formatted_date = date_format($date_obj, 'd-m-Y');
array_push($specific_discount_dates_array, $formatted_date);
endwhile;
}
if (in_array($end_date, $specific_discount_dates_array)) {
return true;
} else {
return false;
}
如果我回显$end_date
,我会得到:28-04-2023
如果我print_r($specific_discount_dates_array)
我得到这个:
Array
(
[0] => 24-04-2023
[1] => 30-04-2023
)
然而,由于某种原因,条件总是返回true,即使28-04-2023不匹配数组中的任何字符串。
我错过了什么?
1条答案
按热度按时间lzfw57am1#
in_array
函数在默认情况下执行非严格比较,这就是为什么它返回true
。使用
in_array
函数中的第三个参数执行严格的比较。