PHP autovivification

跟風遠走 提交于 2019-12-04 03:53:32

From the PHP manual on the square bracket syntax:

$arr[] = value;

If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array

With your example:

$test['a'][4] = 1;

Since $test and $test['a'] don't currently exist; they are both created as arrays.

$test['b'][4]['f'] = 3;

$test['b'] and $test['b'][4] don't currently exist; they are both created as arrays.

$test['a'][4]['f'] = 3;

$test['a'][4] does exist, but it is an integer (1). This is the "scalar value" that cannot be used as an array. You can't use the square bracket [] syntax on number values; it doesn't convert an existing value to an array.

According to the results, PHP has autovivification. The error comes from the way it works.

When you say: $a[1][2] = 55, PHP wants to insert the 55 into $a[1] as [2]=>55. Since the $a[1] is non-existent, PHP is creating automatically an empty node, cca. $a[1] = Array(). But when the node already exists, PHP does not create $a[1], just performs the [2]=>55, which is an error, if $a[1] is not array-like (array, object).

The last language I've seen, where nodes may have value and children too, is MUMPS. There were also a function, called $DATA(), which told wheter a node has any child (10), value (1) or both (11), or it's non-existent (0). I think, that's the correct handling of associative arrays.

(Anyway, I like this behavior of PHP.)

With:

$test['b'][4]['f'] = 3;

You're not adding an element to

$test['a'][4]

bacause it's not initialized as an array.

If you'd write:

$test['a'][4] = array(1);

Then it would work.

With:

$test['a']['b'] = 1;
$test['a']['c'] = 1;
$test['b']['b'] = 1;
$test['b']['c'] = 1;

You're implicitly initializing $test['a'] and $test['b'] as an array. But $test['a']['b'] (and so on) as an int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!