0

If I have the following code:

$a = new stdClass();
$property = 'test';

$a->$property = array();

How can I set a value inside the $a->$property array? The following doesn't work, because [] is applied on $property, not on $a->$property:

$a->$property['key'] = 'value';

Is referencing the property the only way of doing this?

$array = &$a->$property;
$array['key'] = 'value';
0

1 Answer 1

2

You can reference it as $a->{$property}['key'] = 'value';.

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

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.