2015-10-08 153 views
0

如果我想從此代碼中排除2個特定的字段ID,需要更改?從php數組中排除特定值

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

使用'和array_diff()'或'!in_array()',... – Rizier123

回答

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>'; 
} 
+0

輝煌,謝謝! –