简体   繁体   English

Foreach循环的奇怪行为

[英]Strange Behaviour With Foreach Loop

I am experiencing some strange behaviour with the following foreach loop. 我在以下foreach循环中遇到一些奇怪的行为。 I am running it on PHP 5.3.0. 我在PHP 5.3.0上运行它。

print_r($this->form_spec['fields']);
foreach($this->form_spec['fields'] as $f=>$field) {
     print_r($this->form_spec['fields']);                      
}

The first print_r prints the following (note: I've excluded a bunch of keys to make my point clearer): 第一个print_r打印以下内容(注意:为了使我的观点更清楚,我排除了一堆键):

Array
(      
    [0] => Array
    (
       [field_name] => d_first_name
    )

    [1] => Array
        (
            [field_name] => d_last_name
        )
)

The second print_r prints: 第二个print_r打印:

Array
(
    [0] => Array
        (
            [field_name] => d_first_name
        )

    [1] => Array
        (
            [field_name] => d_first_name
        )
)

$this->form_spec['fields'] already has it's values before it reaches the for each loop. $ this-> form_spec ['fields']在到达每个循环之前已经具有它的值。 The first print_r shows the correct values. 第一个print_r显示正确的值。 Then the second print_r shows that the values have changed. 然后,第二个print_r显示值已更改。 There is no other code between these. 这些之间没有其他代码。

Could it be a bug in 5.3.0? 可能是5.3.0中的错误?

The only way I could get round the problem was by using a for loop instead, but the Foreach SHOULD work, it always has done before. 解决这个问题的唯一方法是改用for循环,但是Foreach应该应该一直在做。 Can anybody enlighten me? 有人可以启发我吗?

[EDIT] Just to clarify, the bug is that the contents of $this->form_spec['fields'] changes not the fact that I can't print properly. [编辑]只是为了澄清,错误是$ this-> form_spec ['fields']的内容改变了我不能正确打印的事实。

You are printing $this->form_spec['fields'] instead of the result of the iteration $field . 您正在打印$this->form_spec['fields']而不是迭代$field的结果。

EDIT 编辑

Now that I can see what you mean. 现在我明白了你的意思了。 It looks like you are changing the contents of the object while iterating over it somewhere in your loop. 看来您在循环中某个地方迭代对象时正在更改对象的内容。 Could you please post the relevant code? 您能否发布相关代码?

I'm 99% sure there is more code which is touched while iterating which changes what you have. 我99%的肯定是在迭代更改您所拥有的内容的同时还要触摸更多代码。

You should be doing this:- 您应该这样做:

print_r($this->form_spec['fields']);
foreach($this->form_spec['fields'] as $f=>$field) {
     print_r($field);                      
}

I have run this test on your code:- 我已经在您的代码上运行了此测试:

for($i = 0; $i < 10; $i++){
    $form_spec['fields'][] = array('field_name' => 'field_' . $i);
}
print_r($form_spec['fields']);
foreach($form_spec['fields'] as $f => $field) {
    print_r($form_spec['fields']);
}

(PHP_VERSION = 5.3.0) (PHP_VERSION = 5.3.0)

and got the output I expected, so you must be altering that array somewhere. 并获得了我期望的输出,因此您必须在某个地方更改该数组。 You need to check your code very carefully. 您需要非常仔细地检查代码。

Output:- 输出:-

Array
(
    [0] => Array
        (
            [field_name] => field_0
        )

    [1] => Array
        (
            [field_name] => field_1
        )

    [2] => Array
        (
            [field_name] => field_2
        )

    [3] => Array
        (
            [field_name] => field_3
        )

    [4] => Array
        (
            [field_name] => field_4
        )

    [5] => Array
        (
            [field_name] => field_5
        )

    [6] => Array
        (
            [field_name] => field_6
        )

    [7] => Array
        (
            [field_name] => field_7
        )

    [8] => Array
        (
            [field_name] => field_8
        )

    [9] => Array
        (
            [field_name] => field_9
        )

)

Repeated 10 times. 重复10次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM