Setting multiple checkbox values with form_set_value I have a field that is a taxonomy term reference with a checkbox widget that allows multiple values. 

I am looping through a json structure, finding the associated code value and then setting the value on the checkbox widget in the form with a case statement. 

My problem is it's only setting the last value, not all the values. Here is the code. In this case where the hosts array contains all three values, I would expect all checkboxes associated with all three taxonomy terms to be checked. Only the last one is checked. 

//json structure
"hosts": [
    {
        "alias": "external",
        "url": "abc.com"
    },
    {
        "alias": "internal",
        "url": "abc.com"
    }
    {
        "alias": "local",
        "url": "abc.com"
    }
] 

          //php
          $hosts = $response-&gt;{'hosts'};

           foreach($hosts as $host){

           $alias = $host-&gt;alias;

            switch($alias){
                case "external":
                    form_set_value($form['field_proxy_host'], array(LANGUAGE_NONE =&gt; array(0 =&gt; array('tid' =&gt; '758'))), $form_state);
                break;
                case "internal":
                    form_set_value($form['field_proxy_host'], array(LANGUAGE_NONE =&gt; array(0 =&gt; array('tid' =&gt; '759'))), $form_state);
                break;
                case "local":
                    form_set_value($form['field_proxy_host'], array(LANGUAGE_NONE =&gt; array(0 =&gt; array('tid' =&gt; '760'))), $form_state);
                break;

            }


        }
I believe you want to pass an array with all values to form_set_value() as the function is setting the value, not appending to it. I would alter your setup to build an array of values with your switch/foreach block and then pass that array in a single form_set_value call below the switch, outside of the foreach.

Update your your foreach / switch like so:

$host_values = [];
foreach($hosts as $host){
  switch($host-&gt;alias){
    case "external":
      $host_values[] = ['tid' =&gt; '758'];
      break;
    case "internal":
      $host_values[] = ['tid' =&gt; '759']
      break;
    case "local":
      $host_values[] = ['tid' =&gt; '760']
      break;
    default:
      break;
  }
}

form_set_value($form['field_proxy_host'], [LANGUAGE_NONE =&gt; $host_values], $form_state);