PDA

View Full Version : PHP array into javascript array



gurmeet
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..?

as

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

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

js_array[0]="Apple";
...
js_array[4]="etc";



plz help

djr33
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.)