Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP 0不起作用_Php_Loops_Comparison - Fatal编程技术网

PHP 0不起作用

PHP 0不起作用,php,loops,comparison,Php,Loops,Comparison,我试图在php中根据逗号分隔的值字符串中的一个小时字段设置一个复选框。它适用于1-23小时,但由于某些原因,小时0始终显示为选中状态: <?php myhours = explode(",", substr($arruser['arhours'],0,strlen($arruser['arhours']) - 1)); $checked = ""; for($hour = 0; $hour <= 23; $hour++) { if(count($myhours) > 0

我试图在php中根据逗号分隔的值字符串中的一个小时字段设置一个复选框。它适用于1-23小时,但由于某些原因,小时0始终显示为选中状态:

<?php
myhours = explode(",", substr($arruser['arhours'],0,strlen($arruser['arhours']) - 1));
$checked = "";
for($hour = 0; $hour <= 23; $hour++) {
    if(count($myhours) > 0) {
        for($h = 0; $h <= count($myhours); $h++) {
            if((int)$hour == (int)$myhours[$h]) {
                $checked = " checked ";
                break;
            }
        }
    } else {
        $checked = "";  
    }
    if(strlen($hour) == 1) {
        echo "<td><input type=checkbox " . $checked . " value=" . $hour . " onchange=\"updatehour(" . $_REQUEST['user'] . ",this.checked, '" . $hour . "')\">0$hour:00</td>";   
    } else {
        echo "<td><input type=checkbox " . $checked . " value=" . $hour . " onchange=\"updatehour(" . $_REQUEST['user'] . ",this.checked, '" . $hour . "')\">$hour:00</td>";
    }
    $checked = "";
}
?>

不准确,请回答,但您的代码可以简化为:

$myhours = array(0, 10, 13, 20);
for($hour = 0; $hour <= 23; $hour++) {
    echo '<td><input type="checkbox"' . ( in_array($hour, $myhours) ? ' checked' : '' ) . ' value="' . $hour . '" onchange="updatehour(' . $_REQUEST['user'] . ', this.checked, ' . $hour . ')">' . str_pad($hour, 2, '0', STR_PAD_LEFT) . ':00</td>';   
}
$myhours=数组(0,10,13,20);

对于($hour=0;$hour问题已解决。实际上是
count($myhours)
导致了问题。基本上,即使是空字符串,它仍然返回一个包含1个元素的数组。更改为检查
count($myhours)>1
,并且一切都按预期工作:

<?php
$myhours = explode(",", substr($arruser['arhours'],0,strlen($arruser['arhours']) - 1));
$checked = "";
for($hour = 0; $hour <= 23; $hour++) {
    if(count($myhours) > 1) {
        for($h = 0; $h <= count($myhours); $h++) {
            if((int)$hour == (int)$myhours[$h]) {
                $checked = " checked ";
                break;
            }
        }
    } else {
        $checked = "";  
    }
    if(strlen($hour) == 1) {
        echo "<td><input type=checkbox " . $checked . " value=" . $hour . " onchange=\"updatehour(" . $_REQUEST['user'] . ",this.checked, '" . $hour . "')\">0$hour:00</td>";   
    } else {
        echo "<td><input type=checkbox " . $checked . " value=" . $hour . " onchange=\"updatehour(" . $_REQUEST['user'] . ",this.checked, '" . $hour . "')\">$hour:00</td>";
    }
    $checked = "";
}
?>

问题很简单;看看外部循环:

for ($hour = 0; $hour <= 23; $hour++) {
密切注意回路状况:

$h <= count($myhours)
此时,
$myhours[$h]
是未定义的,因为数组索引超出范围(并且会发出通知),因此
(int)$myhours[$h]
变为
(int)null
,即
int(0)
。因此,当
$hour
的值为
int(0)
时,比较总是正确的

解决方案是将循环条件更改为:

$h < count($myhours)
$h<计数($myhours)

$myhours不是数组数组格式类似于$myhours=array(10,14,20,22);然后它将给出数组的计数,否则它将给出stringModified问题的长度,因为
$myhours
来自数据库中的字符串,然后我使用
explode
函数。修改后的问题正是我如何得到$myhours的。虽然它与您的错误无关,但不应该是因为($h=0;$hif ((int)$hour == (int)$myhours[$h]) {
$h < count($myhours)