I'm trying to add integer references to an array in PHP but for some reason it doesn't work. I am completely confused as to why.
Simplifying things, the code is:
<?php
$myArray = array( 1 => true, 2 => true, 3 => true );
$param_ref = array();
foreach($myArray as $key => $value) {
$param_ref[] = &$key;
}
var_dump($param_ref);
?>
I expect the output to be:
array(3) {
[0] => &int(1)
[1] => &int(2)
[2] => &int(3)
}
But the actual output is:
array(3) {
[0] => &int(3)
[1] => &int(3)
[2] => &int(3)
}
With some closer inspection, it seems the array's ($param_ref) values are being overwritten on each iteration of the loop.
Any idea what's going on?