How can I programmatically create a select input whose values come from a foreign key using Form API? I have Table A who has a foreign key referring to Table B. I want my form associated with Table A to have a select field whose values will come from Table B. I want the select field to have options with key-value pairs coming from Table B.

Both Table A and Table B contain entities of different types.

I'm currently looking for programmatic solutions that use Entity Reference, but without having to go through adding a new field from Administration > Structure > &lt;entity type&gt;.
I went with this solution that uses entity_load then adding the loaded entities as options to the select field for now:

$foreign_entity_items = entity_load('foreign_entity');
$foreign_entity_options = array();
foreach ($foreign_entity_items as $foreign_entity_item) {
    $foreign_entity_options[$foreign_entity_item-&gt;some_field] = $foreign_entity_item-&gt;some_other_field;
}

$form['some_form_element'] = array(
    '#title' =&gt; t('Some title'),
    '#type' =&gt; 'select',
    '#required' =&gt; TRUE,
    '#options' =&gt; $foreign_entity_options
);