如何摆脱错误:Undefined offset: 1 on line 20
?我知道它发生是因为我调用的数组偏移量根本不存在。
用@ 屏蔽它似乎根本不对。
//create an array with all x, y
for ($x = 1; $x <= 5; $x++) $array_x[] = $x;
for ($y = 1; $y <= 5; $y++) $array_y[] = $y;
$IN_x = "'" . implode("', '", $array_x) . "'";
$IN_y = "'" . implode("', '", $array_y) . "'";
$pullMapInfo = "SELECT x, y, value FROM mapinfo WHERE id='{$id}' AND x IN ({$IN_x}) AND y IN ({$IN_y})";
$pullMapInfo2 = mysql_query($pullMapInfo) or die('error here');
//create an associative array x, y => value
while ($pullMapInfo3 = mysql_fetch_assoc($pullMapInfo2)) {
$result[ $pullMapInfo3['x'] ][ $pullMapInfo3['y'] ] = $pullMapInfo3['value'];
}
//loop to display output
foreach ($array_x as $x) {
foreach ($array_y as $y) {
if (array_key_exists($x, $result) && array_key_exists($y, $result)) {
echo '<div class="castle_array" id="'.$x,'x',$y.'">'. $result[$x][$y] .'</div>
';
} else {
echo '<div class="castle_array" id="'.$x,'x',$y.'"></div>
';
}
}
}
2 for 循环似乎很尴尬