0

What would this PHP array look like in JavaScript?

What would be the closest translation?

$nursery_rhyme = array(“mary”, “had”, “a”, “little”, “lamb”);
1
  • 3
    This is not even valid PHP (there are no smart quotes in PHP). Wild guess: copied from your homework? Commented Dec 18, 2010 at 17:21

5 Answers 5

5

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"]
3
var nursery_rhyme=new Array("mary", "had", "a", "little", "lamb");
1
var nursery_rhyme = ["mary", "had", "a", "little", "lamb"];
1
var nursery_ryhme = ["mary", "had", "a", "little", "lamb"];
2
  • 1
    There are no smart quotes in JavaScript. ;-) Commented Dec 18, 2010 at 17:22
  • Haha true. I apparently fail at copy+paste. I wonder if I should even bother fixing the answer, because off all the other 100% equivalent answers. Commented Dec 18, 2010 at 17:32
0

Closest JS translation:

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

Notice:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.