PHP foreach不显示第一个数组值

时间:2016-11-06 03:13:49

标签: php

echo 'SSH Timeout: <select name="ssh_timeout"';
$time = array('1', '5', '15', '30', '60');

foreach ($time as $value) {
    if (15 == $value) {
        echo "<option value='$value' selected>$value seconds</option>";
    } 
    else {
        echo "<option value='$value'>$value seconds</option>";
    }
}

echo '</select>';

该代码未显示1的第一个值,而是从5开始到60。我该如何解决?

1 个答案:

答案 0 :(得分:1)

您应该关闭开始<select标记,否则<option value='1'会进入<select,浏览器将无法呈现它。

这是修复:

echo 'SSH Timeout: <select name="ssh_timeout">';
$time = array('1','5','15','30','60');
    foreach ($time as $value) {
        if (15 == $value) {
            echo "<option value='$value' selected>$value seconds</option>";
        }
        else{
            echo "<option value='$value'>$value seconds</option>";
        }
    }
    echo '</select>';