我需要循环遍历一个数组,但是它不起作用。 此代码有什么问题?
$ForbiddenGrids = array(
"A2A7", "A2A8", "A2B7", "A2B8", "A2C7", "A2C8", "A2D8", "A2E8", "A3A1", "A3A2", "A3A3",
"A3A4", "A3A5", "A3A6", "A3A7", "A3A8", "A3B1", "A3B2", "A3B3", "A3B4", "A3B5", "A3B6",
"A3B7", "A3B8", "A3C1", "A3C2", "A3C3", "A3C4", "A3C5", "A3C6", "A3C7", "A3C8", "A3D1",
"A3D2", "A3D3", "A3D4", "A3D5", "A3D6", "A3D7", "A3D8", "A3E1", "A3E2", "A3E3", "A3E4",
"A3E5", "A3E6", "A3E7", "A3E8", "A3F1", "A3F2", "A3F3", "A3F4", "A3F5", "A3F6", "A3F7",
"A3F8", "A3G2", "A3G3", "A3G4", "A3G5", "A3G6", "A3G7", "A3G8", "A3H3", "A3H4", "A3H5",
"A3H6", "A3H7", "A3H8", "A5A1", "A5A2", "A5A3", "A5A4", "A5A5", "A5A6", "A5A7", "A5A8",
"A5B1", "A5B2", "A5B3", "A5B4", "A5B5", "A5B6", "A5B7", "A5B8", "A5C1", "A5C2", "A5C3",
"A5C4", "A5C5", "A5C6", "A5C7", "A5C8", "A5D1", "A5D2", "A5D3", "A5D4", "A5D5", "A5D6",
"A5D7", "A5E1", "A5E2", "A5E3", "A5E4", "A5E5", "A5E6", "A5E7", "A5F1", "A5F2", "A5F3",
"A5F4", "A5F5", "A5F6", "A5G1", "A5G1", "A5G3", "A5G4", "A5G5", "A5H1", "A5H2", "A5H3",
"A6A1", "A6A2", "A6A3", "A6A4", "A6A5", "A6B1", "A6B2", "A6B3", "A6B4", "A12A3", "A12A4",
"A12A5", "A12A6", "A12A7", "A12A8", "A12B3", "A12B4", "A12B5", "A12B6", "A12B7", "A12B8",
"A12C3", "A12C4", "A12C5", "A12C6", "A12C7", "A12C8", "A12D3", "A12D4", "A12D5", "A12D6",
"A12D7", "A12D8", "A12E4", "A12E5", "A12E6", "A12E7", "A12E8", "A12F4", "A12F5", "A12F6",
"A12F7", "A12F8", "A12G6", "A12G7");
foreach ($ForbiddenGrids as $value) {
if($ForbiddenGrids[$value] == $totalgrid)
{
echo '<script>alert("You can not sail to this direction");</script>';
} else {
$query = "UPDATE playerships SET HGRID1 = '$mainhgrid', HGRID2 = '$varhgrid' WHERE ID='$shipid'";
}
}
当totalgrid具有有效值时,if语句不起作用。我做错了什么?
答案 0 :(得分:3)
其中一个应该是:
foreach ($ForbiddenGrids as $value) {
if($value == $totalgrid)
{
echo '<script>alert("You can not sail to this direction");</script>';
} else {
$query = "UPDATE playerships SET HGRID1 = '$mainhgrid', HGRID2 = '$varhgrid' WHERE ID='$shipid'";
}
}
OR
foreach ($ForbiddenGrids as $key => $value) {
if($ForbiddenGrids[$key] == $totalgrid)
{
echo '<script>alert("You can not sail to this direction");</script>';
} else {
$query = "UPDATE playerships SET HGRID1 = '$mainhgrid', HGRID2 = '$varhgrid' WHERE ID='$shipid'";
}
}
答案 1 :(得分:1)