简体   繁体   English

使用date()和in_array()或array_key_exists()

[英]Using date() with in_array() or array_key_exists()

Here is an array I have to work with: 这是我必须使用的数组:

array ( [0] => monday [1] => thursday [2] => saturday [3] => sunday )

I need to run an if/else statement to check if today's day is in that array. 我需要运行if / else语句来检查今天是否在该数组中。 This does not work 这行不通

// Pretend today is thursdsy
$day = date('l');
$list = array ( [0] => monday [1] => thursday [2] => saturday [3] => sunday );

if ( array_key_exists($day, $list) ) {
    echo "its there";
} else {
    echo "not there";
}

Neither does the in_array() version. in_array()版本也没有。 Not sure if there is a problem using the date() with either of these functions? 不确定将date()与这两个函数一起使用是否存在问题?

If I print_r the $day i get the correct value 'thusday' 如果我将$ day设为print_r,则将获得正确的值“星期天”

Your list has the data in the values instead of the keys.. that is why array_key_exists is failing. 您的列表中的值中包含数据,而不是键中..这就是为什么array_key_exists失败的原因。 Instead use in_array (also since your array week days represented in all lower case... you should do the same with with the supplied $day to get a match) 而是使用in_array(同样因为数组的星期以小写形式表示,所以您应该对提供的$ day做同样的操作以得到匹配项)

if (in_array(strtolower($day), $list){
   // whoop there it is..
} else {
   // not here.
}

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

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