" SplFixedArray重载元素的间接修改没有影响" [英] "Indirect modification of overloaded element of SplFixedArray has no effect"

查看:591
本文介绍了" SplFixedArray重载元素的间接修改没有影响"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下

$a = new SplFixedArray(5);
$a[0] = array(1, 2, 3);
$a[0][0] = 12345; // here
var_dump($a);

产生

Notice: Indirect modification of overloaded element of SplFixedArray has no effect in <file> on line <indicated>

它是一个错误吗?你如何处理与多维SplFixedArrays呢?任何变通办法?

Is it a bug? How do you deal with multidimensional SplFixedArrays then? Any workarounds?

推荐答案

首先,这个问题是关系到实现所有类 ArrayAccess接口这不是一个特殊的问题 SplFixedArray

First, the problem is related to all classes which implement ArrayAccess it is not a special problem of SplFixedArray only.

当您使用 [] 运营商,它的行为不是完全像一个数组访问来自 SplFixedArray 元素。在内部它的 offsetGet()方法被调用,并在您的案件阵列将返回 - 而不是引用以数组。这意味着你就 $所有修改一个[0] 会迷路,除非你将其重新保存:

When you accessing elements from SplFixedArray using the [] operator it behaves not exactly like an array. Internally it's offsetGet() method is called, and will return in your case an array - but not a reference to that array. This means all modifications you make on $a[0] will get lost unless you save it back:

解决方法:

$a = new SplFixedArray(5);
$a[0] = array(1, 2, 3); 
// get element
$element = $a[0];
// modify it
$element[0] = 12345;
// store the element again
$a[0] = $element;

var_dump($a);

下面是使用标这也失败了一个例子 - 只是为了告诉你,这是不相关的只有数组元素

Here is an example using a scalar which fails too - just to show you that it is not related to array elements only.

这篇关于&QUOT; SplFixedArray重载元素的间接修改没有影响&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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