Skip to content

Formula: Getting and setting array and object item values by key with [] syntax #3324

@yurikuzn

Description

@yurikuzn

Applicable only for variables.

Arrays:

$list = list();

$list[0] = '0';
$list[1] = '1';
$list[0 + 2] = '2';

$v = $list[2];

Objects:

$o = object\create();

$o['a'] = 'a';

$v = $o['a'];

Nested access:

$list = list(list());

$list[0][0] = 1;

Reading by a non-existent key results in an error.

Null-coalescing prevents the error:

$list = list();

$v = $list[0] ?? 0;

Metadata

Metadata

Assignees

Labels

improvementImprovement of existing feature

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions