0
Array
(
    [0] => giri
)
Array
(
    [0] => ramya
)
Array
(
    [0] => sangeetha
)
Array
(
    [0] => hemalatha
)
Array
(
    [0] => umar
)

How to convert this PHP array into a Javascript array like below?

var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "BASIC",
    "C",
    "C++",
    "Clojure",
    "COBOL",
    "ColdFusion",
    "Erlang",
    "Fortran",
    "Groovy",
    "Haskell",
    "Java",
    "JavaScript",
    "Lisp",
    "Perl",
    "PHP",
    "Python",
    "Ruby",
    "Scala",
    "Scheme"
];
dda
  • 6,030
  • 2
  • 25
  • 34
  • 5
    With `json_encode`. And try to spend at least 30 seconds formatting your question next time please.. – N.B. Feb 24 '12 at 08:58
  • possible duplicate of [PHP to Javascript Array (Kind of)](http://stackoverflow.com/q/1968977/), [Php pass array to Javascript](http://stackoverflow.com/q/4287654/), [php array to javascript](http://stackoverflow.com/questions/9150382/), and likely many others. In addition to formatting, it's good to spend a little time searching, which cuts down on [noise](http://catb.org/jargon/html/S/signal-to-noise-ratio.html). – outis Feb 24 '12 at 09:44

2 Answers2

2

simple use the json_encode() function.

<?php 
$array = array('element' => 'value', 'element2' => 'value2');
print json_encode($array);
?>
mooncitizen
  • 464
  • 5
  • 10
0

Use json_encode() function to convert PHP data structures to JavaScript arrays/objects.

N.B.
  • 13,688
  • 3
  • 45
  • 55