PHP 自动生存

发布于 2024-11-06 09:27:59 字数 1133 浏览 0 评论 0原文

更新:我提出这个问题的初衷是为了确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-11-13 09:27:59

来自 PHP 手册关于方括号语法:

$arr[] = 值;

如果$arr尚不存在,则会创建它,因此这也是创建数组的另一种方法

例如:

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

由于 $test$test['a'] 当前不存在;它们都被创建为数组。

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

$test['b']$test['b'][4] 当前不存在;它们都被创建为数组。

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

$test['a'][4] 确实存在,但它是一个整数 (1)。这是不能用作数组的“标量值”。您不能对数值使用方括号 [] 语法;它不会将现有值转换为数组。

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.

海的爱人是光 2024-11-13 09:27:59

从结果来看,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.)

南城旧梦 2024-11-13 09:27:59

With:

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

您没有添加元素,

$test['a'][4]

因为它没有初始化为数组。

如果你写:

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

那么它就会起作用。

使用:

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

您将 $test['a']$test['b'] 隐式初始化为数组。但是 $test['a']['b'] (等等)作为 int

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文