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"]
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

2 Comments

There are no smart quotes in JavaScript. ;-)
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.
0

Closest JS translation:

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

Notice:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.