Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Arrays and Control Structures PHP Arrays Associative Quiz

Invalid answer?

How would I add an element to an array with a Key of Orange and Value of fruit, yet array_push($myList, "Orange" => "Fruit") is not valid? how is that incorrect then?

2 Answers

andren
andren
28,558 Points

array_push only accepts values to be pushed on the array. It does not allow you to specify the key the value will end up with. So "Orange" => "Fruit" is an invalid argument to pass to the function.

The correct answer is $myList["Orange"] = "Fruit";. If no element with the key you specify in brackets exist within the array then it will be created.

That clarifies it perfectly! Thanks