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 trialRay Thomas
12,478 Pointssimple php array manipulation help
Without setting the entire array directly, change the element value of "Red" to "Magenta" and the element value of "Blue" to "Cyan".
Why doesn't my code pass?
Thanks.
<?php
$colors = array("Red","Green","Blue");
array_unshift($colors, "Yellow");
array_push($colors, "Black");
unset($colors[1]);
$colors[1] = "Magenta";
unset($colors[3]);
$colors[3] = "Cyan";
1 Answer
Sean T. Unwin
28,690 PointsWhile your way will work, we can use the PHP function splice() which essentially does what you wrote but in one-line.
So instead of
<?php
unset($colors[1]);
$colors[1] = "Magenta";
We would write
<?php
array_splice($colors,1,1,"Magenta");
Where the format is array_splice([Array], [Index to Start for Removal], [Number of Items to Remove], [Replacement Value])
.
Ray Thomas
12,478 PointsRay Thomas
12,478 PointsThank you, Sean! I was expecting to use the same functions outlined in the video just before this assessment. You're the man!
Sean T. Unwin
28,690 PointsSean T. Unwin
28,690 PointsMy pleasure, Ray!
Happy coding.