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

The array does not contain the correct elements

changing array values

index.php
<?php

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

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

1 Answer

andren
andren
28,558 Points

When doing challenges with multiple parts you need to keep the code from each part around. If you remove or modify your previous code then you will usually not be able to complete the task.

So you need to add the code from Task 1 back again. Also since the code from Task 1 adds an item to the start and end of the array you have to change your indexes to reflect that, like this:

<?php

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

//add modifications below this line
array_unshift($colors,"Yellow");
array_push($colors,"Black");

$colors[1] = "Magenta"; // Index increased since "Yellow" was added at the start of the array
$colors[3] = "Cyan"; // Index increased since "Yellow" was added at the start of the array

That will allow you to complete the second task.