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 Modifying Arrays

mahmoud gamie
mahmoud gamie
8,054 Points

I am not able to do it?

what is the correct answer ?

index.php
<?php

$colors = array("Red","Green","Blue");

//add modifications below this line
$colors[0] = "Magenta";
$colors[2] = "Cyan";
?>

2 Answers

So the task asks: Without setting the entire array directly, use a function to add "Yellow" to the beginning of the array. Then add "Black" to the end of the array.

What you tried to do was:

<?php
$colors[0] = "Magenta";
$colors[2] = "Cyan";
?>

What you're doing here would print out "MagentaGreenCyan" What they want is: "MagentaRedGreenBlueCyan"

To do this there are a couple different functions that are learned through the videos called "array_unshift()" and "array_push()". array_unshift() adds a value to the front of the array, while array_push() adds a value to the end.

If you're talking about the second task, you're missing the previous tasks work, in which case gal yamin's answer would be correct.

gal yamin
gal yamin
3,108 Points

$colors[1] = "Magenta"; $colors[3] = "Cyan";