12 Responses to “Creating A JavaScript Array Dynamically Via PHP”
This page contains comments from the Creating A JavaScript Array Dynamically Via PHP article.
This page contains comments from the Creating A JavaScript Array Dynamically Via PHP article.
Stephen Cronin is Manager of Online Service Delivery at a Queensland Government department & has been a freelance WordPress developer/consultant since 2007
*Content on this site is my own and is not related to my employer
Hire me - I'm expensive, but I'm very good!
Want a Custom WordPress plugin? See my Services page.
Visit my home page at Userscripts.org.
And string escaping is the other nasty part since most arrays I’ve had to use cgi trough JS are actually strings. Quotes are the main problem with escaping and I find it best to replce them with character codes for JS.
I have to throw this in there..
PHP has a function for creating json from php objects, json_encode
http://us2.php.net/json_encode
I was about to post this and I agree. This page’s script could be turned into a one liner of .
Since JSON is a subset of JavaScript, JavaScript will already know that it is an array.
Issaac, don’t forget that this function appeared in PHP5, moreover PHP must be higher then 5.2.0. Many people use stll PHP 4.x
Thanks to everyone who suggested json_encode, but I’m with Moder on this one. When I wrote this (almost 2 years ago), I was trying to avoid a dependence on PHP5.
Although I use PHP5 for some of my stuff now, I still try to avoid it where possible for anything related to WordPress (which still supports PHP4).
Why I am not a programmer: I would have padded it with the size of the array, so your first one would have been Array(3,25,34,16), and your second one Array(1,25).
I don’t create theory, I get things outta the ditch.
Hi Ben,
Didn’t see your comment there (from over a year ago)!
Interesting approach! It should work – I can’t see any problem with it – but I’d still stick with an if else statement (or maybe Mike’s implode solution) because … well because the JavaScript is, well, cleaner somehow (I’m not articulating this very well).
I’m not against workarounds to get something working, but only as a last resort.
Thanks for this. The neat handling of single-element arrays is great
Previously, I init. js arrays using the syntax:
var lcValues = new Array(25);
lcValues[1]=’hello’;
lcValues[2]=’goodbye’;
…
but the init format of:
var lcValues = new Array(‘hello’,'goodbye’);
is a lot cleaner.
Maybe I’m missing something cause I read it very fast, but this is how I pass a php array of numbers to a javascript array (very tiny, clean and simple):
With php we don’t have to reinvent the wheel. There are a lot of useful built-in functions, like “implode”.
To make this a little bit more useful… If it’s an array of strings, just do this:
Note: For this last one, check if the php array is not empty. Otherwise you will get a javascript array with one empty string.
Hope it helps
Hi Mike,
That looks like a neat solution. I’ve used implode a lot in the last year or two, but when I wrote this I wouldn’t have been that familiar with it. I’m still not sure whether I would have thought of this approach, but at least it makes sense to me.
By the way, I fixed up the code in your first comment and removed the second one. I’ll have to add a way for users to do this easily.
The simple solution would be to NOT use new Array (whose functionality can be modified by other javascript code) but to just pass the values into square brackets. Javascript accepts [#,#,#] as an unmodified raw array object, even [#], which would solve the problem above altogether without fancy hacks or additional instructions.
Change line 2 part: “\nvar lcValues = new Array(“; to “\nvar lcValues = [";
and line 8 part: ");\n"; to "];\n”;
And problem solved.
Or just use the Javascript array.push method within your loop, which is cleaner still and has the added benefit that it’s easier to read.