Foreach源PHP通过引用传递:最后一个元素,仿型? (错误?) [英] PHP Foreach Pass by Reference: Last Element Duplicating? (Bug?)

查看:104
本文介绍了Foreach源PHP通过引用传递:最后一个元素,仿型? (错误?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚做了一个简单的PHP脚本我正在写一些很奇怪的行为。我把它降低到最低限度的必要重新创建错误:

I just had some very strange behavior with a simple php script I was writing. I reduced it to the minimum necessary to recreate the bug:

<?php

$arr = array("foo",
             "bar",
             "baz");

foreach ($arr as &$item) { /* do nothing by reference */ }
print_r($arr);

foreach ($arr as $item) { /* do nothing by value */ }
print_r($arr); // $arr has changed....why?

?>

此输出:

Array
(
    [0] => foo
    [1] => bar
    [2] => baz
)
Array
(
    [0] => foo
    [1] => bar
    [2] => bar
)

这是一个错误,或者是应该发生了一些非常奇怪的行为?

Is this a bug or some really strange behavior that is supposed to happen?

推荐答案

第一foreach循环之后, $项目还是有些价值的参考它也被用来通过 $ ARR [2] 。因此,在第二循环中,不引用调用每个的foreach调用,替换值,从而 $ ARR [2] ,用新的价值。

After the first foreach loop, $item is still a reference to some value which is also being used by $arr[2]. So each foreach call in the second loop, which does not call by reference, replaces that value, and thus $arr[2], with the new value.

所以环1,价值和 $ ARR [2] 成为 $改编[0] ,这是'富'。结果
回路2,价值和 $ ARR [2] 成为 $改编[1] ,这是'酒吧'。结果
循环3,价值和 $ ARR [2] 成为 $ ARR [2] ,这是'巴'(由于环2)。

So loop 1, the value and $arr[2] become $arr[0], which is 'foo'.
Loop 2, the value and $arr[2] become $arr[1], which is 'bar'.
Loop 3, the value and $arr[2] become $arr[2], which is 'bar' (because of loop 2).

值'巴兹'实际上是失去了在第二foreach循环的第一个电话。

The value 'baz' is actually lost at the first call of the second foreach loop.

有关循环的每次迭代中,我们将回声 $项目的价值,以及递归打印阵列 $改编

For each iteration of the loop, we'll echo the value of $item as well as recursively print the array $arr.

在第一循环润透,我们可以看到这样的输出:

When the first loop is run through, we see this output:

foo
Array ( [0] => foo [1] => bar [2] => baz )

bar
Array ( [0] => foo [1] => bar [2] => baz )

baz
Array ( [0] => foo [1] => bar [2] => baz )

在循环结束时, $项目仍然指向同一个地方 $ ARR [2]

在第二循环润透,我们可以看到这样的输出:

When the second loop is run through, we see this output:

foo
Array ( [0] => foo [1] => bar [2] => foo )

bar
Array ( [0] => foo [1] => bar [2] => bar )

bar
Array ( [0] => foo [1] => bar [2] => bar )

您会发现每一次阵是如何把一个新值 $项目,它也更新 $改编[3] 使用相同的值,因为它们都仍然指向相同的位置。当循环获取到数组的第三个值,它将包含值,因为它只是通过循环的previous重复设置。

You'll notice how each time array put a new value into $item, it also updated $arr[3] with that same value, since they are both still pointing to the same location. When the loop gets to the third value of the array, it will contain the value bar because it was just set by the previous iteration of that loop.

没有。这是一个所指项目的行为,而不是一个错误。这将是类似于运行是这样的:

No. This is the behavior of a referenced item, and not a bug. It would be similar to running something like:

for ($i = 0; $i < count($arr); $i++) { $item = $arr[$i]; }

一个foreach循环在本质上不是在其中可以忽略引用的项目特别。它只是那个变量每段时间设定为新值,像你这样一个循环之外。

A foreach loop isn't special in nature in which it can ignore referenced items. It's simply setting that variable to the new value each time like you would outside of a loop.

这篇关于Foreach源PHP通过引用传递:最后一个元素,仿型? (错误?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆