从 range() 函数中排除数字

Excluding numbers from range() function

我正在用 20 到 60 的数字动态填充 select 框 使用范围 (20, 60)。

<select name="rangenumbers">
    <?php $range = range(20, 60);
        foreach ($range as $range) {
            echo '<option value="'.$range.'">'.$range.'</option>';
        }
    ?>
 </select>

我有一个函数 return 像

这样的数字数组
$a = array(25, 30 , 31, 50);

我需要用 20 到 60 之间的数字填充 select 框,不包括数组中的数字:25、30、31、50。

您可以使用<a href="http://php.net/manual/en/function.array-diff.php" rel="nofollow">array_diff()</a>函数:

<?php
$range = range(20, 60);
$a = array(25, 30 , 31, 50);

// the array_diff() function returns the values in the "$range" array
// that are not present in the array of "$a".
$allRanges = array_diff($range, $a);

foreach ($allRanges as $range) {
    echo '<option value="'.$range.'">'.$range.'</option>';
}
?>