Set option value in a select field The array generated gets me the information that I need, but it's created a keyed array where the key is numeric (0..length-1).  How do I change it so that key is equal to the value, so that the option value is equal to what is displayed?

$page = $sql-&gt;execute();
$pages = array();

    // Populate page dropdown
    foreach($page as $p)
    {
      $pages[] = $p-&gt;title;
    }
// Display only unique values in the array
$paged = array_unique($pages);

// Sort the array in ASC Order
sort($paged);

$form['group_owner_filter']['select'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Filter by Name'),
    '#default_value' =&gt; t('Make a selection...'),
    '#options' =&gt; $option
  );


This generates:

&lt;select id="edit-pages" name="pages" class="form-select"&gt;
    &lt;option value="0"&gt;About Davenport&lt;/option&gt;
    &lt;option value="1"&gt;Academic Support Center&lt;/option&gt;
    &lt;option value="2"&gt;Academics&lt;/option&gt;
    &lt;option value="3"&gt;Adjunct Insurance Scholar Opportunity&lt;/option&gt;
    &lt;option value="4"&gt;Admissions&lt;/option&gt;
So here is the corrected code. I've changed some variable names to make it easier to understand.


$option and $pages became options
$page became $pages
$paged was retired


As @Sujith-Nara mentions you don't need array_unique as the keyed approach will make them unique by default. This does presume that the page titles are unique across the site (which is not enforced). You could replace the key with $p-&gt;nid or $p-name if the titles aren't unique.

Because the keys are now strings you can use ksort instead of sort to order them on the key.

$pages = $sql-&gt;execute();
$options = array();

// Populate page dropdown
foreach($pages as $p)
{
    $options[$p-&gt;title] = $p-&gt;title;
    // $options[$p-&gt;name] = $p-&gt;title;
    // $options[$p-&gt;nid] = $p-&gt;title;
}

// Sort the array alphabetically in ASC Order
ksort($paged);

$form['group_owner_filter']['select'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Filter by Name'),
    '#default_value' =&gt; t('Make a selection...'),
    '#options' =&gt; $options
);