PHP 自动生存
更新:我提出这个问题的初衷是为了确定 PHP 是否真的具有此功能。这在答案对标量问题的关注中被忽略了。请参阅这个新问题:"Does PHP has autovivification?" 这个问题留在这里供
根据维基百科,PHP 没有autovivification,但此代码有效:
$test['a']['b'] = 1;
$test['a']['c'] = 1;
$test['b']['b'] = 1;
$test['b']['c'] = 1;
var_dump($test);
输出:
array
'a' =>
array
'b' => int 1
'c' => int 1
'b' =>
array
'b' => int 1
'c' => int 1
我发现此代码也有效:
$test['a'][4] = 1;
$test['b'][4]['f'] = 3;
但添加此行会导致警告(“警告:无法将标量值用作数组”)
$test['a'][4]['f'] = 3;
这里发生了什么?为什么在索引后面添加关联元素时会失败?这是“真正的”类似 Perl 的自动生存,还是它的某种变体,还是其他东西?
编辑:哦,我现在看到标量的错误,哎呀!这些按预期工作:
$test['a'][4]['a'] = 1;
$test['a'][4]['b'] = 2;
$test['a'][5]['c'] = 3;
$test['a'][8]['d'] = 4;
那么,php 确实有自动生存功能吗?在 Google 中搜索“php autovivification”不会出现规范的答案或示例。
Update: My original intention for this question was to determine if PHP actually has this feature. This has been lost in the answers' focus on the scalar issue. Please see this new question instead: "Does PHP have autovivification?" This question is left here for reference.
According to Wikipedia, PHP doesn't have autovivification, yet this code works:
$test['a']['b'] = 1;
$test['a']['c'] = 1;
$test['b']['b'] = 1;
$test['b']['c'] = 1;
var_dump($test);
Output:
array
'a' =>
array
'b' => int 1
'c' => int 1
'b' =>
array
'b' => int 1
'c' => int 1
I found that this code works too:
$test['a'][4] = 1;
$test['b'][4]['f'] = 3;
But adding this line causes an warning ("Warning: Cannot use a scalar value as an array")
$test['a'][4]['f'] = 3;
What's going on here? Why does it fail when I add the associative element after the index? Is this 'true' Perl-like autovivification, or some variation of it, or something else?
Edit: oh, I see the error with the scalar now, oops! These work as expected:
$test['a'][4]['a'] = 1;
$test['a'][4]['b'] = 2;
$test['a'][5]['c'] = 3;
$test['a'][8]['d'] = 4;
So, php does have autovivification? Searching Google for "php autovivification" doesn't bring up a canonical answer or example of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 PHP 手册关于方括号语法:
例如:
由于
$test
和$test['a']
当前不存在;它们都被创建为数组。$test['b']
和$test['b'][4]
当前不存在;它们都被创建为数组。$test['a'][4]
确实存在,但它是一个整数 (1
)。这是不能用作数组的“标量值”。您不能对数值使用方括号[]
语法;它不会将现有值转换为数组。From the PHP manual on the square bracket syntax:
With your example:
Since
$test
and$test['a']
don't currently exist; they are both created as arrays.$test['b']
and$test['b'][4]
don't currently exist; they are both created as arrays.$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.从结果来看,PHP具有自动生存功能。错误来自于它的工作方式。
当你说:$a[1][2] = 55时,PHP想要将55插入到$a[1]中,作为[2]=>55。由于 $a[1] 不存在,PHP 自动创建一个空节点,cca。 $a[1] = 数组()。但是当节点已经存在时,PHP不会创建$a[1],只是执行[2]=>55,如果$a[1]不是类数组(数组,对象),这是一个错误。
我见过的最后一种语言是 MUMPS,其中节点可能具有值,并且子节点也可能具有值。还有一个名为 $DATA() 的函数,它告诉节点是否有子节点 (10)、值 (1) 或两者都有 (11),或者不存在 (0)。我认为,这是关联数组的正确处理方式。
(无论如何,我喜欢 PHP 的这种行为。)
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['a']
和$test['b']
隐式初始化为数组。但是$test['a']['b']
(等等)作为int
With:
You're not adding an element to
bacause it's not initialized as an array.
If you'd write:
Then it would work.
With:
You're implicitly initializing
$test['a']
and$test['b']
as an array. But$test['a']['b']
(and so on) as anint