Skip to content Skip to sidebar Skip to footer

How To Convert This Php Array Into A Javascript Array?

What would this PHP array look like in JavaScript? What would be the closest translation? $nursery_rhyme = array(“mary”, “had”, “a”, “little”, “lamb”);

Solution 1:

As others mentioned, the curly/smart quotes are invalid.

You can get the JavaScript representation of most simple PHP structures by running the data through json_encode():

php> echo json_encode(array("mary", "had", "a", "little", "lamb"))
["mary","had","a","little","lamb"]

Solution 2:

var nursery_rhyme=newArray("mary", "had", "a", "little", "lamb");

Solution 3:

var nursery_rhyme = ["mary", "had", "a", "little", "lamb"];

Solution 4:

var nursery_ryhme = ["mary", "had", "a", "little", "lamb"];

Solution 5:

Closest JS translation:

$nursery_rhyme = newArray("mary", "had", "a", "little", "lamb");

Notice:

  • no var is needed
  • you can still use the $

Post a Comment for "How To Convert This Php Array Into A Javascript Array?"