title: Generating a javascript array in php with "Foreach" Loop
body: <p>So I'm generating a javascript array of objects in php with a <code>for</code> loop.  My code looks somewhat like this:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  var items = [ 
  &lt;?php foreach($items as $item): ?&gt;
       {
          "title" : "&lt;?php echo $item-&gt;title ?&gt;",
          "image" : "&lt;?php echo $item-&gt;getImage()?&gt;",
        }, 
   &lt;?php  endforeach ?&gt;
  ];
  
  &lt;/script&gt;
  </code></pre>
  
  <p>This code will not work, since I end up with an extra comma at the end of my javascript array.  Is there an elegant way to deal with that comma that separates the javascript objects?</p>