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

Andres Vicente
Andres Vicente
1,665 Points

Without setting the entire array directly, remove the element with the value "Green".

no longer passed. i dont know wat is wrong with my code.

index.php
<?php

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

//add modifications below this line
array_unshift($colors, "Yellow");
array_push($colors, "Black");
$colors[1] = "Magenta";
$colors[3] = "Cyan";

array_pop($colors,  2,  1);
Andrew Dovganyuk
Andrew Dovganyuk
10,633 Points

use the code

unset($colors[2]);

Thats how you remove the value whatch again https://teamtreehouse.com/library/removing-array-elements

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Andres,

In last challenge, you are calling the wrong array function. 'array_pop()' removes the last item from the array, not an item at the specified index. Try 'array_splice()' instead:

<?php

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

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

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

array_splice($colors,  2,  1);
Damien Watson
Damien Watson
27,419 Points

And yes, as Andrew said, 'unset()' also does the trick. :)

Samandar Mirzayev
Samandar Mirzayev
11,834 Points

u have to check by var_dump($colors) and u will see what's wrong there