在数组中查找匹配变量

问题描述:

I want to determine if $today is a known non-working day or holiday by comparing two arrays and report the result. I do not know what belongs in the if and elseif structures to compare if the variable within the array is a match to $today:

<?php
// Date & Time
$timeadjust = '-5 hours';
$today_long = date('l, d F Y', strtotime($timeadjust));
$today = date('m-d', strtotime($timeadjust));
$today_day_name = date('l', strtotime($timeadjust));
// Normal Non-Working Days
$saturday = 'Saturday';
$sunday = 'Sunday';
// 2015 Holidays
$new_years_day = '01-01';
$fourth_of_july = '07-04';
$thanksgiving = '11-26';
$thanksgiving_friday = '11-27';
$christmas_eve = '12-24';
$christmas = '12-25';
$new_years_eve = '12-31';
// Normal Non-Working Day Array
$no_work = array($saturday,$sunday);
// Holiday Array
$holiday = array($new_years_day,$fourth_of_july,$thanksgiving,$thanksgiving_friday,$christmas_eve,$christmas,$new_years_eve);
// Compare Today To Normal Non-Working Day & Holiday Arrays To Determine If Today Is Normal Non-Working Day Or Holiday
if ($today_day_name = $no_work) {$operating = 'CLOSED';}
elseif ($today = $holiday) {$operating = 'CLOSED';}
else {$operating = 'OPEN';}
// Display Result Of Comparison & Report Operating Status
echo '<h3>Today is&nbsp;'.$today_long.'.&nbsp;We are&nbsp;'.$operating.'&nbsp;today!</h3>';
?>

我想确定 $ today code>是否为已知的非工作日或假日 比较两个数组并报告结果。 我不知道 if code>和 elseif code>结构中有什么属于比较数组中的变量是否与 $ today code>匹配: p>

 &lt;?php 
 // Date&amp;  Time 
 $ timeadjust ='-5 hours'; 
 $ today_long = date('l,d F Y',strtotime($ timeadjust)); 
 $ today = date('m-d',strtotime($  timeadjust)); 
 $ today_day_name = date('l',strtotime($ timeadjust)); 
 //正常非工作日
 $ saturday ='Saturday'; 
 $ sunday ='Sunday'; \  n // 2015年假期
 $ new_years_day = '01 -01'; 
 $ fourth_of_july = '07 -04'; 
 $ thanksgiving = '11 -26'; 
 $ thanksgiving_friday = '11 -27'; \  n $ christmas_eve = '12 -24'; 
 $ christmas = '12 -25'; 
 $ new_years_eve = '12 -31'; 
 //正常非工作日数组
 $ no_work = array($ 星期六,$ sunday); 
 // Holiday数组
 $ holiday =数组($ new_years_day,$ fourth_of_july,$ thanksgiving,$ thanksgiving_friday,$ christmas_eve,$ christmas,$ new_years_eve); 
 //今日比较普通非  - 工作日&amp; 假日数组确定今天是否正常非工作日或假日
 
($ today_day_name = $ no_work){$ operating ='CLOSED';;} 
elseif($ today = $ holiday){$ operating ='CLOSED';}  
else {$ operating ='OPEN';} 
 //显示比较结果&amp; 报告运行状态
echo'&lt; h3&gt;今天是&amp; nbsp;'。$ today_long。'。&amp; nbsp;我们正在'$。运营。'&amp; nbsp;今天!&lt; / h3&gt;';  
?&gt; 
  code>  pre> 
  div>

For what you're doing you can use in_array() which will test if the value is in a selected array -

// Compare Today To Normal Non-Working Day & Holiday Arrays To Determine If Today Is Normal Non-Working Day Or Holiday
if (in_array($today_day_name, $no_work)) {
    $operating = 'CLOSED';
} elseif (in_array($today, $holiday)) {
    $operating = 'CLOSED';
} else {
    $operating = 'OPEN';
}

EXAMPLE

In your original code you're also assigning instead of testing -

if($foo = $bar) // one equals sign assigns
if($foo == $bar) // two equals signs compares
if($foo === $bar) // three equals signs tests for equivalency, does it match value and type?

Because you were assigning your if statement would always evaluate to true.