View Full Version : PHP array into javascript array

11-04-2009, 04:59 AM
how i can convert PHP array into javascript array....?

i have some text values that i retrieve from MYSQL throught PHP and want to store into an array and want that array to be converted into javascript array... how i can achieve it..?


$phparray=array("Apple","mango","banana","grapes","etc") /// values from databse to php array

///////it must be in javascript array


plz help

11-04-2009, 04:47 PM
Loop through all of the (keys and) values, and echo them line by line.

foreach($array as $key=>$value) {
echo "js_array['$key'] = '$value';\n";

That's a starting point anyway. PHP is an html/javascript generator, so use it to generate the same JS you would write by hand.

(\n is a new line in the code above. The rest should be clear enough.)