Results 1 to 2 of 2

Thread: PHP array into javascript array

  1. #1
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default PHP array into javascript array

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Loop through all of the (keys and) values, and echo them line by line.

    PHP Code:
    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.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •