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!