PHP数组引用的异常行为 [英] Unexpected behaviour with PHP array references

查看:102
本文介绍了PHP数组引用的异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用引用来更改数组:

I'm using references to alter an array:

foreach($uNewAppointments as &$newAppointment)
{
    foreach($appointments as &$appointment)
    {
        if($appointment == $newAppointment){
            $appointment['index'] = $counter;
        }
    }
    $newAppointment['index'] = $counter;
    $newAppointments[$counter] = $newAppointment;

    $counter++;
}

如果我打印数组内容,那么我会收到预期的结果.当我对其进行迭代时,所有元素似乎都是相同的(第一个).

If I print the array contents, then I receive the expected result. When I iterate over it, all elements seem to be the same (the first).

当我删除内部数组中的引用运算符& 时,一切正常,除了未设置索引.

When I remove the reference operator & in the inner array, all goes normal, except index isn't set.

推荐答案

如果执行此操作,则退出循环时必须取消设置$ newAppointment.这是相关条目.

If you do this, you must unset $newAppointment when you exit the loop. Here is the relevant entry.

这篇关于PHP数组引用的异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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