There are lots of suggestions in the comments that are better than my original solution. Also, with 5 years of hindsight, I’d approach it differently anyway! The way I’d do this now would be as follows:
So much simpler! PHP4 has almost disappeared so there’s no reason not to use json_encode. I’d recommend that you stop reading now and just go with that.
My Original Solution
var lcValues = new Array(25,34,16);
The Problem – Arrays With A Single Element
The problem occurs when there is only one value to convert. For example, if $lc_values only contains 25, this will create:
var lcValues = new Array(25);
You cannot use new Array() to create an array with a single numeric element.
var lcValues = new Array(1); lcValues=25;
The final code I used looks like this:
There are obviously different ways to do this, but this is what works for me. If it helps anyone else, great! If anyone knows a better way to do this, let me know in the comments.