我必须用PHP制作一个名为"Lights Out"的小游戏,我首先要制作一个5x5矩阵,每个元素都必须是0到1之间随机选择的数字。
稍后,当我想处理我的文件并访问无线电输入中选择的选项(打开或关闭)时,我必须知道无线电指示在哪个小区。我试图将以下内容添加到我的值中:[$row][$column]
,但它似乎不起作用(错误非法偏移类型)。
有人能告诉我为什么会出现这个错误吗?我真的需要知道收音机在哪一行哪一列。
提前感谢!
<form action="Processing.php" method="post">
<?php
$matrix = array(
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1))
);
echo "<table border='"1'">"; // echo matrix in a html table
foreach ($matrix as $row) { // rows
echo "<tr>";
foreach ($row as $column) { // columns
echo "<td>";
echo $column . "<br />";
echo "<input type='"radio'" name='"I_O'" value='"O $matrix[$row][$column]'" /> O"; // off
echo "<br />";
echo "<input type='"radio'" name='"I_O'" value='"I $matrix[$row][$column]'" /> I"; // on
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?></form>
您的foreach
结构只为您提供值,但您将它们用作数组的索引。我已经稍微成功了(还没有测试),但这应该会帮助你:
<form action="Processing.php" method="post">
<?php
$matrix = array(
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1)),
array(rand(0,1), rand(0,1), rand(0,1), rand(0,1), rand(0,1))
);
echo "<table border='"1'">"; // echo matrix in a html table
foreach ($matrix as $rowIx => $row) { // rows
echo "<tr>";
foreach ($row as $columnIx => $val) { // columns
$checked = $val ? 'checked="checked" ' : '';
echo "<td>";
echo $val . "<br />";
echo "<input type='"radio'" name='"I_O'" $checked value='"{$rowIx},{$columnIx}'" />";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?></form>
edit:使单选按钮的值以逗号分隔成行和列的索引矩阵。