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 trialChris Gauthier
Courses Plus Student 6,434 PointsWorking with Arrays
I'm being asked to change the elements in the array $colors from the element Red to Magenta and the element Blue to Cyan.
My code is below, it won't pass the compiler and i can't figure out why, it works perfectly in my local php environment.
Thanks Chris
<?php
$colors = array("Red","Green","Blue");
//add modifications below this line
array_unshift($colors,"Yellow");
array_push($colors,"Black");
$colors[0] = 'Magenta';
$colors[2] = "Cyan";
1 Answer
Corey Cramer
9,453 PointsMessing around with the test it looks like your array indexes may be off. Shows 1 and 3 for me where you're setting 0 (Yellow) and 2 (Green).
array(5) { [0]=> string(6) "Yellow" [1]=> string(3) "Red" [2]=> string(5) "Green" [3]=> string(4) "Blue" [4]=> string(5) "Black" }
Is it possible that you're looking at the indexes before adding Yellow? That would have the indexes lined up properly.
Chris Gauthier
Courses Plus Student 6,434 PointsChris Gauthier
Courses Plus Student 6,434 PointsAh ok, I think I get it. I added "yellow" to the top of the list with array_unshift function, so Red is no longer at position 0, but rather position 1. And, similarily, "Blue" is at position 3, instead of position 2.
Perfect, makes complete sense now.
Thanks :)
Corey Cramer
9,453 PointsCorey Cramer
9,453 PointsGlad I could help. For fun if you were feeling extra lazy you could implement it like this: