从php数组中排除特定值

时间:2015-10-08 21:06:16

标签: php

如果我想从此代码中排除2个特定字段ID,需要更改什么?

// foreach field, print the option
echo '<select name="sort" id="pref_sortby">';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
    echo '<optgroup label="'.get_string('fields', 'data').'">';
    foreach ($fields as $field) {
        if ($field->id == $sort) {
            echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
        } else {
            echo '<option value="'.$field->id.'">'.$field->name.'</option>';
        }
    }
    echo '</optgroup>';
}

1 个答案:

答案 0 :(得分:0)

// foreach field, print the option
echo '<select name="sort" id="pref_sortby">';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
    echo '<optgroup label="'.get_string('fields', 'data').'">';
    foreach ($fields as $field) {
        // add this condition, replacing 50 and 60 with the IDs you want to exclude
        if (in_array($field->id, array(50, 60))) {
            continue;
        }
        if ($field->id == $sort) {
            echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
        } else {
            echo '<option value="'.$field->id.'">'.$field->name.'</option>';
        }
    }
    echo '</optgroup>';
}