views:

606

answers:

5

I want to be able to do the following:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

// Here I want to access the $normal_array reference **as a reference**,
// but that doesn't work obviously. How to do it?
end( $array_of_arrayrefs )["one"] = 1; // choking on this one

print $normal_array["one"]; // should output 1

Regards

/R

A: 

The line:

end( $array_of_arrayrefs )["one"] = 1; // choking on this one

throws a parse error: "Parse error: syntax error, unexpected '[' in /file.php on line 65" Make sure you have error_reporting and display_error activated.

I'm not sure what you want to do but this works:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );
// Here I want to access the $normal_array reference **as a reference**,
// but that doesn't work obviously. How to do it?
$array_of_arrayrefs[0]["one"] = 1;
//end($array_of_arrayrefs )["one"] = 1; // choking on this one
print $normal_array["one"]; // should output 1
Joe Scylla
+2  A: 

end() doesn't return a reference of the last value, but rather the last value itself. Here is a workaround:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

$refArray = &end_byref( $array_of_arrayrefs );
$refArray["one"] = 1;

print $normal_array["one"]; // should output 1

function &end_byref( &$array ) {
    $lastKey = end(array_keys($array));
    end($array);
    return $array[$lastKey];
}
Andrew Moore
A: 

Here are a couple of approaches, neither of which I find particularly satisfying. I'm sure there's a better way..

<?php
$normal_array       = array();
$array_of_arrayrefs = array( "blah", &$normal_array );

foreach ($array_of_arrayrefs as &$v);
$v["one"] = 1;

echo $normal_array["one"];  //prints 1
?>


<?php
$normal_array       = array();
$array_of_arrayrefs = array( "blah", &$normal_array );

$lastIndex = @end(array_keys($array_of_arrayrefs)); //raises E_STRICT because end() expects referable.
$array_of_arrayrefs[$lastIndex]["one"] = 1;

echo $normal_array["one"];  //prints 1
?>
rewbs
+1  A: 

You probably shouldn't be passing by reference in the first place. It's generally considered bad practise to do so, because it makes it hard to see where state gets modified.

It's a very common misconception that references are faster. This is not the case - In fact, they are a little bit slower, but it's by such a small amount, that it really doesn't matter. PHP has a system called copy-on-write, which means that variables aren't actually copied, before you write to them.

The only place where you really need references, were in PHP4, where objects would get cloned otherwise. This is not needed in PHP5.

troelskn
A: 

The function end() doesn't just return a value. It also moves the array's internal pointer. Then we can use key() to get the index, after which we're able to use regular array access for the assignment.

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

end($array_of_arrayrefs);
$array_of_arrayrefs[ key($array_of_arrayrefs) ]["one"] = 1;

print $normal_array["one"];
Preston