Is there a way of referencing an array key from within the array? This may make more sense in code format:
$array=array(
"Key1"=>array(
"Value1",
"Value2"
),
"Key2"=>&$this['Key1']
);
What I want is for $array['Key2'] to output the same as $array['Key1']. I can add $array['Key2']=&$array['Key1']; after the array is created, but would like to keep it all in one code block if possible.
I’ve checked the docs on references, as well as some of the suggest similar questions here and a search for “php array reference”.
The answer to this, as it turns out, is Yes. However it is not a tidy syntax as it uses a sort of sub-statement, and leaves the current scope littered with an extra reference variable.
Consider the following code:
I was surprised that this worked with no errors, but it does – here’s the proof. Of course, you wouldn’t do this, but you can…