5

I have the following code:

$array = array(
    array("name"=>"John"),
    array("name"=>"Rob"),
    array("name"=>"Tom"),
);
foreach($array as &$el)
    $el["age"]=23;

foreach($array as $el)
    echo $el["name"] . " is: " . $el["age"] . " years old<br>";

I'm expecting something like this:

John is: 23 years old
Rob is: 23 years old
Tom is: 23 years old

but I actually receive the following:

John is: 23 years old
Rob is: 23 years old
Rob is: 23 years old

Why is that ? In the first foreach loop I'm using reference to edit the source array.

ArturoO
  • 611
  • 7
  • 19

0 Answers0