dongya8378 2013-11-18 16:53
浏览 126
已采纳

“间接修改SplFixedArray的重载元素没有任何影响”

Why the following

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

produces

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

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

  • 写回答

4条回答 默认 最新

  • dongzhu6900 2013-11-18 16:54
    关注

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


    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:

    Workaround:

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制