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

Rex Irvin Carpen
Rex Irvin Carpen
3,314 Points

I just modify how come task 1 is no longer correct anymore

I just modify how come task 1 is no longer correct anymore?

index.php
<?php

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

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

array $colors["Red"] = "Magenta";
array $colors["Blue"] = "Cyan";


?>

1 Answer

Matthew Lang
Matthew Lang
13,483 Points

First of all, you do not need the 'array' keyword when editing the array. For example, you can simply do: $array[1] = "x";

$colors["Red"] = "Magenta"; This line of code is looking for the value with key of "Red" in the $colors array and changes it to Magenta. There is no set keys in the $colors array, therefore it uses an incrementing value from 0. For example, $colors[0] == "Yellow" (after task 1, anyway)

Here is my code, that works.

<?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";
Rex Irvin Carpen
Rex Irvin Carpen
3,314 Points

thanks Mathew Lang. I already get it now.