php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55772 is_array throws E_NOTICE on undefined array elements, isset does not.
Submitted: 2011-09-24 00:29 UTC Modified: 2011-09-24 19:06 UTC
From: rick dot foos at solengtech dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.8 OS: Fedora/Ubuntu
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rick dot foos at solengtech dot com
New email:
PHP Version: OS:

 

 [2011-09-24 00:29 UTC] rick dot foos at solengtech dot com
Description:
------------
I have a sparse matrix, that needs each element to display on an html page. So when the matrix is loaded with defined values there are many undefined values, 6158 for example.

This is displayed as a square matrix using an HTML table - A conflict between good PHP programming and HTML programming. The undefined elements need some html, so the 'for loops' must touch each element of the array to display the square set of values.

is_array is the correct call to use. It tests array elements, and returns true or false if they are valid or not.

While the is_array function returns true and false - PHP still issues the warning message. is_array is a test, not an operation on a null value, and therefore not bad programming requiring a message.

http://rickfoosusa.blogspot.com/2011/09/howto-clear-php-undefined-offset.html

Test script:
---------------
$data_array=array();
$data_array[2][10] = "defined";
$row_count = 10;
$col_count = 10;

//...fill with the defined values...many values undefined.

for ($r = 0; $r < $row_count; $r++)
{
echo "<tr>";
  for ($c = 0; $c < $col_count; $c++)
  {
     if (isset($data_array[$r+1][$c+1])){
       $data = $data_array[$r+1][$c+1];
     } else {
       $data = NULL;
     }
    echo "<td>";
  }
} 

Expected result:
----------------
True or False depending if the array element is valid.

Actual result:
--------------
True or False depending if the array element is valid.

PHP Notice:  Undefined offset: 118 in /usr/share/resultsdb/www/index.php on line 465, referer: http://134.86.96.216/resultsdb/index.php?


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-24 15:14 UTC] anonymous at nowhere dot never
>is_array is the correct call to use.

What are you blithering about? I think you mean isset(). is_array has nothing to do with it.

Anyway, the problem you're having is because you're treating $data_array as a multidimensional array. It's not. PHP has arrays of arrays, not multidimensional arrays (in languages like this you could make a triangular array if you wanted). So if the row is not defined then testing isset on the row's column will complain about the undefined row. So you have to isset on the row to make sure that's there first before you can test its column elements.

E.g.:

if (isset($data_array[$r + 1]) && isset($data_array[$r + 1][$c + 1])) ...

Or like this:

for ($r = 0; $r < $row_count; $r++) {
	echo "<tr>";
	if (isset($data_array[$r + 1])) {
		$row = $data_array[$r + 1];
		for ($c = 0; $c < $col_count; $c++) {
			if (isset($row[$c + 1])) {
				$data = $row[$c + 1];
			} else {
				$data = null;
			}
			echo "<td>", $data;
		}
	} else {
		echo str_repeat("<td>", $col_count);
	}
}
 [2011-09-24 16:08 UTC] rick dot foos at solengtech dot com
Sorry for blithering, long day, and most of the comments on arrays involved lectures. isset works, is_array returns true/false correctly, but throws an E_NOTICE.

is_array is a test, and should not throw an error when the test is false.
 [2011-09-24 19:06 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2011-09-24 19:06 UTC] aharvey@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

is_array() tests the type of the given variable. It doesn't test
whether the variable itself exists -- it assumes it does, and checking
that is the job of isset().

Also, anonymous, should you check back in: perhaps you could avoid
describing reports made in good faith as "blithering". And put a name
to your comments, although I can understand why you might want to
disown that one.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Apr 27 13:01:27 2025 UTC