Creating JS objects in PHP with commas in between
<p>I'm trying to create an array of JS objects from a PHP array, but I'm struggling to find a way to insert commas in between each object.</p>

<p>Here's what I'm trying to output:</p>

<pre><code>var things = [
    {
        a: "foo",
        b: "bar"
    },  // Comma on this line
    {
        a: "moo",
        b: "car"
    }   // No comma on this line
];
</code></pre>

<p>And here is what I have so far:</p>

<pre><code>var things = [
    &lt;?php foreach ($things as $thing): ?&gt;
    {
        a: "&lt;?php echo $thing-&gt;getA(); ?&gt;",
        b: "&lt;?php echo $thing-&gt;getB(); ?&gt;"
    }
    &lt;?php endforeach; ?&gt;
];
</code></pre>

<p>I suppose I could resort to something ugly, like an <code>if</code> statement that only runs once:</p>

<pre><code>&lt;?php
    $i = 1;
    if ($i == 1) {
        echo '{';
        $i++;
    } else {
        echo ',{';
    }
?&gt;
</code></pre>

<p>Is there not a cleaner/better way to do this?</p>
