如何从两个数组集中“检查”匹配的日期


How to 'checked' the matched day from two array set

我需要通过选中数据库中的复选框与周数组来匹配日期。这是我的资源:

$weekDay

$weekDay=array("1"=>"Mon","2"=>"Tue","3"=>"Wed","4"=>"Thu","5"=>"Fri","6"=>"Sat","7"=>"Sun");

$opDay

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 )

所以我写:

foreach($weekDay as $key=>$val){
$inDay = array_intersect($weekDay,$opDays);        
echo "<label><input type='"checkbox'" name='"exc_opd[]'" value='"{$key}'" ".(($inDay)? '"checked"':" " )."/>&nbsp;{$val}</label>&nbsp;";                    
}

根据函数,我希望与$opDay匹配的复选框会checked。循环将继续未选中的框。

最后,我想出了这个解决方案。可能不是最好的,但它正在工作。任何关于我的知识建议将不胜感激。

foreach($weekDay as $key=>$val){
$inDay = array_intersect_key($weekDay,array_flip($opDays));        
if($inDay[$key]){
    echo "<label><input type='"checkbox'" name='"exc_opd[]'" value='"{$key}'" checked/>&nbsp;$val</label>&nbsp;";       
}else{
    echo "<label><input type='"checkbox'" name='"exc_opd[]'" value='"{$key}'"/>&nbsp;$val</label>&nbsp;";       
    }
}