dplo59755 2013-07-18 16:09
浏览 51
已采纳

在已洗牌的数组上使用unset()的意外行为

This is test code designed to take an array of arrays, shuffle them, and assign the values to a ['data'] element in a parent array, then to delete one element. It's very basic just to illustrate a point:

$g = //some array;

shuffle($g);
foreach ($g as &$r) {
    shuffle($r);
}
$tables[] = array('name' => 'table1', 'data' => $g);

shuffle($g);
foreach ($g as &$r) {
    shuffle($r);
}
$tables[] = array('name' => 'table2', 'data' => $g);

shuffle($g);
foreach ($g as &$r) {
    shuffle($r);
}
$tables[] = array('name' => 'table3', 'data' => $g);

unset($tables[0]['data'][0][0]);

print_r($tables);

When $g is an array with more than one element, and the nested arrays have more than one element, it works fine. The first value in the first nested array in the data element is deleted:

$g = array('12' => array('11111', '22222'), '56' => array('55555', '66666'));

// Output
Array
    [0] => Array
        [name] => table1
        [data] => Array
            [0] => Array
                [1] => 66666
            [1] => Array
                [0] => 11111
                [1] => 22222
    [1] => Array
        [name] => table2
        [data] => Array
            [0] => Array
                [0] => 11111
                [1] => 22222
            [1] => Array
                [0] => 55555
                [1] => 66666
     [2] => Array
         [name] => table3
         [data] => Array
            [0] => Array
                [0] => 55555
                [1] => 66666
            [1] => Array
                [0] => 11111
                [1] => 22222

When $g is an array with one element, the first element in all data elements is removed, which is not expected:

$g = array('12' => array('11111', '22222'));

// Output
Array
    [0] => Array
        [name] => table1
        [data] => Array
            [0] => Array
                [1] => 22222
    [1] => Array
        [name] => table2
        [data] => Array
            [0] => Array
                [1] => 22222
    [2] => Array
        [name] => table3
        [data] => Array
            [0] => Array
                [1] => 22222

$g = array('12' => array('11111'));

// Output
Array
    [0] => Array
        [name] => table1
        [data] => Array
            [0] => Array
                    (Empty)
    [1] => Array
        [name] => table2
        [data] => Array
            [0] => Array
                    (Empty)
    [2] => Array
        [name] => table3
        [data] => Array
            [0] => Array
                    (Empty)

I can't see how this could be expected behavior. And it's been a long day. So can someone put me out of my misery and tell me what I'm missing here?

Thanks.

Later: Just saw this which may be my answer:

PHP shuffle not working like expected on my nested array

Later again:

Yes, it was. Added unset($r) after the foreach() loops and it works fine. No replies needed!

  • 写回答

1条回答 默认 最新

  • douqiang5809 2013-07-18 16:20
    关注

    Just saw this which provided the answer:

    PHP shuffle not working like expected on my nested array

    So just added unset($r) after the foreach() loops and it works fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C