2

I was wondering...is it possible to use a value from an array which is currently being declared? Something like:

$a = array(
    'foo' => 'value',
    'bar' => $a['foo']
);

This is only a simple example. It would be pretty useful doing this, since it frees you from doing extra manipulation after the array declaration.

0

2 Answers 2

4

No, you can't, but you can do something like :

$a = array(
    'foo' => ($val = 'value'),
    'bar' => $val
);
Sign up to request clarification or add additional context in comments.

1 Comment

An interesting approach. Although you use an extra variable, you at least don't have to manipulate the array after. Seems to be the best approach possible in this situation.
1

No. $a['foo'] will only be available after the assignment completes entirely.

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.