3

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

6 Answers 6

4

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];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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
?>

Comments

1

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.

Comments

0

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"];

Comments

0

To be honest, your demonstration either doesn't reflect the reality of the use case or this is an XY Problem.

Regardless of that preamble, from PHP7.3, you can access the last key of an array without moving the pointer.

Code: (Demo)

$normal_array       = [];
$array_of_arrayrefs = [&$normal_array];
$array_of_arrayrefs[array_key_last($array_of_arrayrefs)]["one"] = 1;

echo $normal_array["one"]; // outputs: 1

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.