我创建了这个<select>
标签,其中如果<option>
值等于一定数量,它不会出现,显示或打印这所学校的项目。
例如:如果一组数字是1,2,4,5,9则只有具有3,6,7,8,10,11,12值将显示<option>
标签。
<select name="">
<?php
$set_of_numbers = "1,2,4,5,9";
for($i=0; $i<=12; $i++) {
if($i != $set_of_numbers) {
echo '<option value='.$i.'>'.$i.'</option>';
}
}
?>
</select>
你必须能够检查在设定数字编程,就像这样:
<select name="">
<?php
$set_of_numbers = [1, 2, 4, 5, 9];
for ($i = 1; $i <= 12; $i++) {
if (!in_array($i, $set_of_numbers)) {
echo '<option value='.$i.'>'.$i.'</option>';
}
}
?>
</select>
如果您set of numbers
是也只能是一个string
,那么你可能会像这样的东西去:
$set_of_numbers = "1,2,4,5,9";
$set_of_numbers = explode(',', $set_of_numbers); // This makes an array of the numbers (note, that the numbers will be STILL stored as strings)
如果你希望能够比较数作为整数,解决办法是:
$set_of_numbers = "1,2,4,5,9";
$set_of_numbers = json_decode('[' . $set_of_numbers . ']'); // This creates a valid JSON that can be decoded and all of the numbers WILL be stored as integers
希望,你有这个:)
执行以下更改代码,这应该工作。
$set_of_numbers = array(1,2,4,5,9)
...
if (!in_array($i, $set_of_numbers))
您可以使用和array_diff只得到不在列表中的号码。
$set_of_numbers = "1,2,4,5,9";
$numbers = explode(",", $set_of_numbers);
$range = range(1,12);
$numbers_to_output = array_diff($range, $numbers);
// [3,6,7,8,10,11,12]
foreach($numbers_to_output as $n){
echo '<option value='.$n.'>'.$n.'</option>';
}
这样,你只循环要呼应值。 其他方法将循环所有值,需要每个值进行比较,以你的号码清单。
的代码可以在被冷凝:
foreach(array_diff(range(1,12), explode(",",$set_of_numbers)) as $n){
echo '<option value='.$n.'>'.$n.'</option>';
}