Modifying Array Elements

An item in an array can be changed by specifying its position and providing a new value, like this:

$myArray = array("red", "blue", "yellow");

echo $myArray[1];
// outputs "blue"

$myArray[1] = "green";

echo $myArray[1];
// outputs "green"
  1. First we create a new array $myArray with a list of colors.
  2. Then we output the item at position 1. Since items are numbered starting from 0, "blue" is at position 1
  3. Next we change the item at position 1 to "green".
  4. Now when we output the item at position 1, we get "green".

In line 8, there's an array named $languages.

  1. Change an item in the $languages array. You can take your pick!
  2. Then use echo to output $languages.
Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

Modifying array elements is as easy as selecting them with [] or {} notation and assigning them a new value with =!

View Preview