找到你要的答案

Q:Exclude specific value(s) from php array

Q:排除特定的值(S)从PHP数组

If I want to exclude 2 specific field ids from this code, what needs to change??

// 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>';
}

如果我想从这个代码中排除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>';
}
answer1: 回答1:
// 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>';
}
// 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>';
}
php