相关疑难解决方法(0)

PHP数组引用的意外行为

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

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

    $counter++;
}
Run Code Online (Sandbox Code Playgroud)

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

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

php foreach loops pass-by-reference

0
推荐指数
1
解决办法
296
查看次数

标签 统计

foreach ×1

loops ×1

pass-by-reference ×1

php ×1