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 trialRiki Tiilikainen
4,042 PointsHow 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); ?>
<?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
Alexander Davison
65,469 Points
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
J J
15,775 PointsThis worked for me
array_pop($colors, 2, 1);
Melissa Venter
6,197 Pointsunset($colors[2]);
Jason Anello
Courses Plus Student 94,610 PointsHi 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
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
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsThose functions all exist in php.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsOooh lol I thought Riki Tiilikainen was calling the
splice
method on anarray
haha :DThanks!