使用函数通过引用更改 PHP 多维数组中的值 我想将所有 0 值更改为 1 到 6 之间的随机数
我使用 $_SESSION 来保存骰子卷
/*MY ARRAY LOOKS LIKE THIS
Array
(
[0] => Array
(
[save_1] => Array
(
[pos1] => 0
[pos2] => 2
[pos3] => 0
[pos4] => 0
[pos5] => 3
[pos6] => 0
)
)
[1] => Array
(
[save_2] => Array
(
[pos1] => 0
[pos2] => 6
[pos3] => 0
[pos4] => 5
[pos5] => 1
[pos6] => 0
)
)
[2] => Array
(
[save_3] => Array
(
[pos1] => 2
[pos2] => 0
[pos3] => 5
[pos4] => 0
[pos5] => 2
[pos6] => 2
)
)
[3] => Array
(
[save_4] => Array
(
[pos1] => 0
[pos2] => 6
[pos3] => 6
[pos4] => 5
[pos5] => 5
[pos6] => 0
)
)
[4] => Array
(
[save_5] => Array
(
[pos1] => 2
[pos2] => 0
[pos3] => 4
[pos4] => 4
[pos5] => 0
[pos6] => 0
)
)
[5] => Array
(
[save_6] => Array
(
[pos1] => 3
[pos2] => 0
[pos3] => 4
[pos4] => 0
[pos5] => 5
[pos6] => 5
)
)
)
*/
//I TRIED THIS
function foo(&$var) {
foreach ($var as $key) {
foreach ($var as $key) {
foreach ($var as $key => $value) {
$value = mt_rand(1,6);
}
}
}
}
unset($var); // break the reference
foo($diceRoll);