php-array 2 javascript-array
Why this script
This script is particularly usefull in combiation with Ajax functions.
For example: you can retrieve data from the database with a php script, serialize the row you retrieved from the database and output this. This output can be loaded into ajax and with these handy functions you can turn them into javascript arrays.
Usage
Beware, I was in desperate need of a script like this when i wrote it, so as soon as it proved to be working i never gave it a second glance anymore :-)
So, I'm sure it could improve a lot still. If you have any recommendations, please use the
feedback form to let me (and everyone else) know.
This is the code used in the
example, when you click on the
Show Button:
<script type="text/javascript">
function init(serString)
{
elShow = document.getElementById('debug');
elShow.innerHTML = serString;
var arr = phpArr2jsArr(serString);
elShow.innerHTML = showObj(arr, 0);
}
</script>
serString is, of course, a string, representing a serialized php-array. And the only thing you need to do, is feed it to the phpArr2jsArr-function. The returned result is a javascript array!
Beware
Using this script you must be very careful not to mix encodings. If you serialize (in php) text encoded in one standard and then start creating an array from that (in javascript) in another, you're bound to get some errors sooner or later.
These errors typically happen with special characters.
Note: besides not being very elegantly written, this function is also very poorly commented on. I'll try to update that asap.