Fix for database helper function for keyed array

  1. /**
  2.  * Database helper function: fetch the entire result set as a keyed array.
  3.  * The first field in the result becomes the keys.
  4.  * If there is only one other field, it becomes the flat values.
  5.  * If there is more than one remaining field, these become the value as a keyed array.
  6.  * This is useful for populating form checkboxes: SELECT value,label.
  7.  */
  8. function db_fetch_keyed_array($result) {
  9.   while ($row = db_fetch_array($result)) {
  10.     $key = array_shift($row);
  11.     if (count($row) <= 1) {
  12.       $keyed_array[$key] = array_shift($row);
  13.     }
  14.     else {
  15.       $keyed_array[$key] = $row;
  16.     }
  17.   }
  18.   return $keyed_array;
  19. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.