title: Set Select2 selected values with JSON string
body: <p>I have a select2 that's used to add multiple employees to a work permit. Adding the employees works with no issues but when the permit is viewed I need the added employees to be pre-selected on the select2. I can retrieve the values from the DB via ajax using a JSON string but the select2 only shows each value one at a time rather than as a multi select. Multiple is set to true when the select2 is generated.</p>
  
  <p>My HTML page code is below</p>
  
  <pre><code>&lt;legend&gt;&lt;h5 class="text-semibold"&gt;Employees&lt;/h5&gt;&lt;/legend&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-8 col-md-offset-2"&gt;
          &lt;div id="select_employee_div" class="content-group-lg"&gt;
              &lt;label class="control-label text-bold"&gt;Select Employees *&lt;/label&gt;
              &lt;select id="select_employee" class="form-control multi-select-search"&gt;
  
              &lt;/select&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>My select2 initialisation code is below </p>
  
  <pre><code>// Select with search
  $('.multi-select-search').select2({
      multiple: true
  });
  </code></pre>
  
  <p>My ajax code is below.</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: 'php_files/permit_php_files/permit_employee_process.php',
      dataType: 'JSON',
      data: {
          permit_id: $('#permit_id').val()
      },
      success: function(data) {
  
      var my_obj = data;
      $.each(my_obj, function (i, z) {
  
              $("#select_employee").val(my_obj[i].employee_id).trigger('change');
          });
     }
  });
  </code></pre>
  
  <p>My PHP Code is below</p>
  
  <pre><code>$stmt = $mysqli-&gt;prepare("SELECT employee_id FROM tbl_permit_employee WHERE permit_id = ?");
  $stmt-&gt;bind_param("i", $permit_id);
  $stmt-&gt;execute();
  $stmt-&gt;store_result();
  $stmt-&gt;bind_result($employee_id);
  $row_array = array();
  
  while($stmt-&gt;fetch()) {
      $tmp = array();
      $tmp['employee_id'] = $employee_id;
      array_push($row_array, $tmp);
  }
  $stmt-&gt;close();
  
  echo json_encode($row_array);
  </code></pre>