41

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".

7
  • 6
    No you can't. While creating the array, there is no possible reference point yet. So you do have to add the reference afterwards. Commented Apr 27, 2012 at 21:48
  • 1
    +1 for an interesting question :-) Commented Apr 27, 2012 at 21:50
  • 1
    There is no way to do it in a single statement, unless you reference something that already exists. So you could create $ref = array("Value1", "Value2"); and then do $array = array('Key1'=>&$ref, 'Key2'=>&$ref); and unset($ref); afterwards - but there's no point, you'd just do the thing you suggested and create the reference afterwards. Commented Apr 27, 2012 at 21:52
  • 4
    I am intrigued as to the usage. Could you put some context around this please? - what is the scenario where you need to use this? - purely for reference but maybe it could highlight an alternative to your requirements... Commented Apr 27, 2012 at 21:57
  • 1
    Usage is for a potential minigame to a project whereby if a character has a weapon in left hand and nothing in the other, their class is the same as if they had a weapon in left hand and shield in right. $class=array( "W"=>array( "S"=>array( /* Figure out the class */ ), "N"=>&$class['W']['S'] ) ); That was the plan, anyway. Commented Apr 28, 2012 at 7:01

2 Answers 2

31

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:

<?php

  $array = array(

    // Creates Key1 and assigns the value to it
    // A copy of the value is also placed in $ref
    // At this stage, it's not a reference
    "Key1"=>($ref = array(
      "Value1",
      "Value2"
    )),

    // Now Key2 is a reference to $ref, but not to Key1
    "Key2"=>&$ref,

    // Now everything is referenced together
    "Key1"=>&$ref

  );

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

Sign up to request clarification or add additional context in comments.

6 Comments

+1 This works because when you declare the same key twice in an array, it overwrites the 1st one. :-)
Indeed this is apparently the case, I did wonder if it would complain since it seems quite an odd thing to do and I have never actually tried it before. Obviously one quite commonly reassigns keys in an array individually, but rarely in the declaration statement!
@Starx: Remove the comments and whitespace. This is only one statement.
@Starx Technically it is two statements, but nested into one.
Thanks for taking the time to post. Looks like it'll do the trick, and is easy enough on the eyes that I won't forget what I was doing in x years time.
|
1

Impossible to make it in one block, because you not initialized the variable yet. Same with class variables. To do such thing, you will need to create any variable any way, and than use its links only, but its using memory, so once again, real answer to your question is - impossible :)

Comments

Your Answer

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