Skip to content

Instantly share code, notes, and snippets.

@vibhasbhingarde
Created May 30, 2014 18:53
Show Gist options
  • Save vibhasbhingarde/faf415fbac1aa7c2bb59 to your computer and use it in GitHub Desktop.
Save vibhasbhingarde/faf415fbac1aa7c2bb59 to your computer and use it in GitHub Desktop.
Convert assoc array with id and title array to ci_dropdown array
function array_to_select($results, $value = 'id', $key = 'title', $add_blank=false)
{
// Converts objects to arrays
if(is_object($results)) $results = get_object_vars($results);
$options = array();
if(!empty($add_blank)) $options = array(null=>$add_blank);
// Will only run if results is an array, not a string, int, etc.
if(is_array($results))
{
foreach($results as $result)
{
// Get the two rows specified
$options[$result[$value]] = $result[$key];
}
}
return $options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment