This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Arrays Forum

View Course

61 points
Picture
Submitted by
Shahe Imran Imon
over 2 years ago

Why can not I change the existing value with a new one?

I got a problem in "Modifying Array Element" Session. It says "An item in an array can be changed by specifying its position and providing a new value."

But the problem is when i try to set a new value by specifying one's position, The result is showing both existing and the new value.

Even the example codecademy showing Us also expressing the same result.
You can try this by yourself.

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

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

$myArray[1] = "green";

echo $myArray[1];
// outputs "green"
?>


0 votes

permalink

delete the echo $myArray[1]; and it won't output the blue anymore.

967 points
A2289f83082f49cf3ee2cffd6447a856?s=140&d=retro
Submitted by
vakho777vakho
over 2 years ago


0 votes

permalink

the first echo request the value of $myArray[1] is blue so its outputs blue, you then change the value and echo again, this time the value is green.

780 points
55dcb19395e378e83600022f_404522167
Submitted by
Jason Rez
over 2 years ago