title: convert php associative array into javascript object
body: <p>I'm trying to load Chinese words as keys, and their English translations as values from a database into a php array so then I can use them on the client side in JavaScript. So I load the PHP key:value pairs into the JavaScript array and try to output the results as key value pair as such:</p>
  
  <pre><code>stuff : Ni, You 
  stuff : Ta, Him or Her
  stuff : Wo, I
  </code></pre>
  
  <p>Chinese and English words are loaded in a relational database.</p>
  
  <p><strong>PHP</strong>:</p>
  
  <pre><code>$wordsArray = array();               
  while ($row = $sql-&gt;fetch_assoc()) {
      $wordsArray[$row['chinese']] = $row['english'];
  }
  </code></pre>
  
  <p><strong>Javascript</strong>: Here I want the $.each to output the key as a string, and not a number index. So when I tried <code>var words = [&lt;?php echo '"'.implode('","', $wordsArray).'"' ?&gt;];</code> as an array, I got:</p>
  
  <pre><code>stuff : 0, You 
  stuff : 1, Him or Her
  stuff : 2, I
  </code></pre>
  
  <p>When I'm really looking for:</p>
  
  <pre><code>stuff : Ni, You 
  stuff : Ta, Him or Her
  stuff : Wo, I
  </code></pre>
  
  <p>So I changed <code>words</code> to be an object so that <code>$.each</code> could output key as string:</p>
  
  <pre><code>var words = {&lt;?php echo '"'.implode('","', $wordsArray).'"' ?&gt;};
  $.each(words, function(key, value) {
      console.log('stuff : ' + key + ", " + value);
  });
  </code></pre>
  
  <p>Which throws error: <code>SyntaxError: Unexpected token ,</code></p>