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

Vijay Ramdass
Vijay Ramdass
13,275 Points

PHP - Modifying Arrays Quiz (Question 2)

Without setting the entire array directly, change the element value of "Red" to "Magenta" and the element value of "Blue" to "Cyan".

I am getting this error: Bummer! The array does not contain the correct elements.

I did a var_dump and everything seems to be in order. Here is my code:

<?php

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

//add modifications below this line

array_unshift($colors, "Yellow"); array_push($colors, "Black");

array_shift($colors); $colors[0] = "Magenta";

array_pop($colors); $colors[2] = "Cyan";

var_dump($colors);

11 Answers

Got it! It's because we added Yellow and Black, so now we need to target index 1 and 3! :)

Waldo Alvarado
Waldo Alvarado
16,322 Points

Remember, the index value of Red and Blue increased by one because you added Yellow to the beginning of the array.

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

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

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

This is starting to make sense. I was going as far as looking up splicing. Just need to remind myself to keep it simple.

Vijay Ramdass
Vijay Ramdass
13,275 Points

Thank you for your reply. The unshift and push at the beginning for colors Black, and Yellow.

That is the answer I originally had, however with that I am getting this error:

Bummer! Change the element with the value "Red" to the value "Magenta"

I'm on to the third part of the question where we need to remove the value green. I think unset($colors, 2); should do this but I keep getting the following error.

Oops! It looks like Task 1 is no longer passing

Vijay Ramdass
Vijay Ramdass
13,275 Points

Close! Try doing it this way:

Unset($array[key]);

Here's my code that passes step 1 and 2. The 2 '$colors' lines are step 2. Note that the index has changed as we did add an element to the top in step 1. <?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";

?>

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

Not sure why you're doing all these shifts and unshifts, but here's the easiest way to do this:

$colors = array("Red","Green","Blue");
//add modifications below this line
$colors[0] = "Magenta";
$colors[2] = "Cyan";
John Ireland
John Ireland
6,585 Points

The first challenge has you adding things to the array, therefore changing the index. if you change that to

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

.....you're good to go.

Well we need to be aware that the index values for "Red" has shifted from 0 to 1 and that of "Blue" from 2 to 3. This is because we have attached string "Yellow" to the front, index 0.

Thanks Vijay - schoolboy error that one!

Matthew Costigan
Matthew Costigan
18,319 Points

Help!

<?php

$colors = array("Red","Green","Blue"); array_push($colors, "Black"); array_unshift($colors, "Yellow");

//add modifications below this line $colors[0] = "Magenta"; $colors[2] = "Cyan";

Can't understand why this is not passing ! and getting the error Bummer! Change the element with the value "Red" to the value "Magenta"

Anne Donald
Anne Donald
9,847 Points

Thank you, I was struggling with this!

<?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";

var_dump($colors);

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

Hie Vjay Ramdas, here is the correct answer for both challenge 1 and 2 from Masango Dewheretsoko

<?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"; ?>

natapol chanpradab
natapol chanpradab
3,190 Points

$colors = array("Red","Green","Blue"); array_push($colors,'Black'); array_unshift($colors,'Yellow');

$replacements = array(1 => "Magenta", 3 => "Cyan"); $colors = array_replace($colors, $replacements);