samedi 18 octobre 2014

Why does Referencing to Array Elements not work in this situation?


Vote count:

0




This referencing works:



$awesome_array = array (1,2,3);
$cool_array = array (4,5,6);

$ref = &$awesome_array; // reference awesome_array
$awesome_array = $cool_array;
echo $ref; //produces (4,5,6) as expected


This referencing also works:



$array[0] = "original";
$element_reference = &$array[0]; // reference $array[0]
$array[0] = "modified";
echo $element_reference; // returns "modified" as expected.


But referencing the elements in an array does not work when you change the entire array. How do you get around this?



$array = array (1,2,3);
$new_array = array (4,5,6);

$element_reference = &$array[0]; // reference $array[0]
$array = $new_array; // CHANGE ENTIRE ARRAY
echo $element_reference; // returns 1 despite the fact that the entire array changed. I need it to return 4?


Why does it not return 4 since the array has changed? How do you reference the element so it returns 4?



asked 1 min ago







Why does Referencing to Array Elements not work in this situation?

Aucun commentaire:

Enregistrer un commentaire