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

Riki Tiilikainen
Riki Tiilikainen
4,042 Points

How do I get do I complete this task?

Without setting the entire array directly, remove the element with the value "Green". How do I get do I complete this task?

<?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(2); ?>

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_splice($colors, 2);
?>

5 Answers

array_unshift, array_push, and array_splice All aren't pre-defined.

To call a method on anything, you must do it this way:

thing.method

NOT this way:

thing_method

Good luck! ~alex

Those functions all exist in php.

Oooh lol I thought Riki Tiilikainen was calling the splice method on an array haha :D

Thanks!

This worked for me

array_pop($colors,  2,  1);

Hi Riki,

array_splice takes an optional 3rd argument which is how many elements to remove. http://php.net/manual/en/function.array-splice.php

Without specifying this it will remove all the way to the end. In this case, you only want it to remove 1 element.

array_splice($colors, 2, 1);
Samandar Mirzayev
Samandar Mirzayev
11,834 Points

<?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'; unset($colors[2]); ?>

this is a correct way. u have to show which part do u wanna remove