Possible Duplicate:
Generating a JavaScript array from a PHP array
I just want to know how to create JavaScript array with the help of PHP and help are definitely appreciated
I have a PHP result to this form
Array
(
[0] => stdClass Object
(
[id] => 11
[name] => MLB
[totalcount] => 4
)
[1] => stdClass Object
(
[id] => 35
[name] => American Idol
[totalcount] => 2
)
)
and i want to convert this array to JavaScript array like this
<script type="text/javascript">
var myData = new Array(['MLB', 4], ['American Idol', 2]);
</script>
First, convert your input array to the required format:
Now you can enjoy the benefits of
json_encode: http://php.net/manual/en/function.json-encode.phpBy the way, you don’t have to write
new Array(...), simply write[...], just like your inner arrays.I think I answered your question, see http://codepad.org/5guUWmHD