Multiple select not working in drupal8 I have a form element as follows :

$form['fieldset']['select-user'] = array(
            '#type' =&gt; 'select',
            '#title' =&gt; t('Select a user'),
            '#options' =&gt; select_user_dropdown_options(),
            '#description' =&gt; t('Select a user'),
            '#size' =&gt; 10,
            '#attributes' =&gt; array(
                'style' =&gt; 'width:600px',
                'multiple' =&gt; 'true'
            )
        );


And in the form submit handler i am processing it as follows :

    /**
     * #submit callback for node_adoption_transfer_form().
     */
    public function submitForm(array &amp;$form, FormStateInterface $form_state) {
        $groupId = $form_state-&gt;getValue('select-user-group');
        $groupName = $form_state-&gt;getValue('group_name');
        $userId = $form_state-&gt;getValue('select-user');
        $groupName = empty($groupName) ? '' : $groupName;
        #print_r($userId);exit;
        foreach ($userId as $key =&gt; $value) {
            $query = "select * from {usergroup_user_map} where user_id=$value AND group_id ='$groupId'";
            if (count(db_query($query)-&gt;fetchObject()) &gt; 0) {
                continue;
            } else
                db_query("insert into {usergroup_user_map} (user_id,group_name,group_id) values ($value,'$groupName','$groupId')");
        }
     }


But when am printing the $userId its only printing the last value , How to resolve this ?

UPDATE


  my issue solved .I was using '#multiple' => 'true' instead of
  '#multiple' => TRUE , my bad ;) . That saved my day ..
I had same issue , but i solved like this

$form['fieldset']['select-user'] = array(
   '#type' =&gt; 'select',
   '#title' =&gt; t('Select a user'),
   '#options' =&gt; select_user_dropdown_options(),
   '#description' =&gt; t('Select a user'),
   '#size' =&gt; 10,
   '#multiple' =&gt; TRUE,
   '#attributes' =&gt; array(
      'id' =&gt; 'edit-select-user',
      'style' =&gt; 'width:600px',
   )
);


Code:


Form:


Output in submit: