php 比较字符串与数组中的值以返回true或false

46scxncf  于 2023-04-28  发布在  PHP
关注(0)|答案(1)|浏览(113)

我有这个代码:

$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不匹配数组中的任何字符串。
我错过了什么?

lzfw57am

lzfw57am1#

in_array函数在默认情况下执行非严格比较,这就是为什么它返回true
使用in_array函数中的第三个参数执行严格的比较。

if (in_array($end_date, $specific_discount_dates_array, true)) {
    return true;
} else {
    return false;
}

相关问题