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


Riccardo Busin
Riccardo Busin
11,741 Points

I have doubts about the answer to a question of a test

Hi, the test asks me: "How would I add an element to $myList with a KEY of "Orange" and a value of "Fruit"?"

and i selected the answer : "array_push($myList, "Orange" => "Fruit")" but the correct answer was "$myList["Orange"] = "Fruit"."

Since the test asks me to ADD and not to REPLACE why "array_push($myList, "Orange" => "Fruit")" isn't correct?

Thanks for reading and I wish you a good day.

3 Answers

Hi Riccardo,

array_push can't be used to push a key/value pair onto an array. It can only push a value onto the array and it will be given an integer index for the key.

Micah Doulos
Micah Doulos
17,663 Points

I found this confusing as well, but basically, array_push(variable, "value"); is used for single value arrays. And Variable[key] = value; is used for key/value paired arrays. $myList["Orange"] = "Fruit"; would add Orange/Fruit to the end of the array if it did not exist in the array, or it would replace whatever value Orange had with Fruit, if Orange did exist in the array.

Chet Callahan
Chet Callahan
8,648 Points

We don't learn this technique until a few videos after this task (Mixing Data Types in Arrays), a fairly common occurrence in various Treehouse courses unfortunately