title: Using PHP array for jQuery autocomplete
body: <p>I have a list of guests that I want to be able to use in the jQuery autocomplete but I can't seem to get the format correct. I'm using CakePHP 2.3.8 and have a list of guests sent from my controller to view. When I type into the text box, I get the following error </p>
  
  <pre><code>//in reference to source of the elements to populate the autocomplete list
  Property 'source' of object [object Object] is not a function 
  </code></pre>
  
  <p>I searched for this error on here, but I couldn't find anything related to converting a PHP array while also accounting for the index number of that item. I have a PHP array in the format of</p>
  
  <pre><code>echo "&lt;pre&gt;"; print_r($guest_list); echo "&lt;/pre&gt;"; 
  //results in the following, where the index is the guest's ID
  array(
      [1] =&gt; guest a,
      [2] =&gt; guest b,
      [3] =&gt; guest c
  )
  </code></pre>
  
  <p>Keep in mind, the index refers to the guest's ID.</p>
  
  <p>I used json_encode to convert the php $guests array which results in the following format</p>
  
  <pre><code>var guests = {"1":"guest a","2":"guest b","3":"guest c"};
  </code></pre>
  
  <p>I noticed in the documentation the source of the data is usually in a format like</p>
  
  <pre><code>var guests = [guest_a, guest_b, guest_c];
  </code></pre>
  
  <p>Keep in mind index refers to the guest's ID, so I need to use this number to make sure the correct guest is chosen, if there happens to be two guests with the same name</p>
  
  <p>So, here's the javascript/jquery for the autocomplete box</p>
  
  <pre><code>var guests = &lt;?php echo json_encode($guest_list); ?&gt;;
  $(function() {
      $( "#guests_autocomplete" ).autocomplete({
        source:  guests
      });
  });
  </code></pre>
  
  <p>How can I properly format my guests array to work with autoComplete and still keep track of the guest's id?</p>