How to get rid of PHP Undefined offset error?

How to get rid of error: Undefined offset: 1 on line 20? I know it occurs becaouse Im calling array that's offset simply does not exist.

Silecing it wiht @ just doesnt seem right at all.

//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 loops seems awkard

Answers


Your second call to array_key_exists should pass in $result[$x]:

if ( array_key_exists($x, $result) && array_key_exists($y, $result[$x]) ) {
    // Code goes here...
}

Replace this line:

if (array_key_exists($x, $result) && array_key_exists($y, $result)) {

with:

if (isset($result[$x][$y])) {

Documentation: isset

For Joseph Silber: Try this PHP fiddle


Need Your Help

Not authorized to perform sts:AssumeRoleWithWebIdentity AWS s3 Cognito auth failure

ios amazon-web-services amazon-s3 amazon-cognito

I have a simple iOS app that uploads to s3. I'm trying unauth all around- even though ideally I want to do Facebook.

How to get document in print queue

c# winforms winapi printing windows-services

Is there any way in C# to get the documents sent to the printer from another program and use those documents again for something else?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.