2
此引用作品:爲什麼在這種情況下引用數組元素不起作用?
$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
此引用也適用:
$array[0] = "original";
$element_reference = &$array[0]; // reference $array[0]
$array[0] = "modified";
echo $element_reference; // returns "modified" as expected.
但在引用數組中的元素,當你改變整個陣列不起作用。你如何解決這個問題?
$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?
爲什麼它不返回4因爲數組已經改變?你如何引用元素,使其返回4?