So I'm generating a javascript array of objects in php with a for
loop. My code looks somewhat like this:
<script type="text/javascript">
var items = [
<?php foreach($items as $item): ?>
{
"title" : "<?php echo $item->title ?>",
"image" : "<?php echo $item->getImage()?>",
},
<?php endforeach ?>
];
</script>
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?