SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying php array in javascript

    Hi,

    Can i display php array variale in javascript?

    I have an array in php:

    eg. $color['1'] = "red";
    $color['2'] = "brown"; ....etc.

    I want to use this array in javascript

    eg. alert($color[code]) .Here code is javascript variable.

    Thanks & Regards,
    Viji.

  2. #2
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str '<script type="text/javascript" language="javascript">';
     
    $str .= 'var colors = new Array()';
     for (
    $i  0$n count($colors); $i $n$i++) {
         
    $str .= 'colors[' $i '] = ' $colors[$i];
         
    //or $str .= 'colors.push(' . $colors[$i] . ');';
     
    }
     
    $str .= '</script>';
     
     echo 
    $str


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
  •