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

Are we able to update the value in the array by using array_splice this method?

I did try two of the methods to actually resolve the problem I met in this challenge task 2 of 3. The first one I try is using array_splice method to actually replace the element with the specific index number and the value the questions given. The second method is just simple assign the array[number] to the new value. But both methods are not able to pass this challenge. Any help will be appreciate! Thanks!

index.php
<?php

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


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

//Method 1
array_splice($colors,0,"Magenta");
array_splice($colors,2,"Cyan");

//Method 2
array["Red"] = "Magenta";
array["Blue"] = "Cyan";
?>

1 Answer

Hi WK h,

With array_splice you need the 3rd argument to be how many items you're removing which is 1 in this case. The 4th argument is the new color value. You also have to adjust your indices. "Red" is not at index 0 anymore and "Blue" is not at index 2

It's easier to do method 2 though but the index is integer based. You have to specify the integer index for "Red" and for "Blue" keeping in mind that "Yellow" was added to the beginning.

Oh, I forgot that Black and Yellow are in the array! Thank you for point it out!! Appreciate your help!!