vibhasbhingarde
5/30/2014 - 6:53 PM

Convert assoc array with id and title array to ci_dropdown array

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