Forum Moderators: coopster

Message Too Old, No Replies

How to test for existance of an array?

         

audiolizard

6:39 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



This seemingly simple question is driving me nuts. I have a dynamically generated form that includes a series of checkboxes with names and values assigned to an array. The problem happens when no checkboxes are generated, and the array isn't defined. The code is expecting an array. How do I test if the array exists or not, so that I can bypass the code that references the array if the array doesn't exist? With variables, you can use isset(), but that doesn't work with an array, only specific elements in an array.
Warning: isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.

The array_key_exists() function is closer, but again, I'm trying to test the existance of the whole array, not a particular key. If the array doesn't exist at all, I get an error:
Warning: array_key_exists(): The second argument should be either an array or an object

I can't find a similar function that tests for the existence of an array or any other way to do it, but I'm sure there's an easy way... anybody? :(

coopster

7:48 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



An array is a variable. Don't confuse a variable with types [php.net] of variables (scalars, arrays, etc). You can indeed use isset to test for the existence of an array.
$array = array(); 
if (isset($array)) {
print 'true'; // prints this one.
} else {
print 'false';
}

Also, if you know the array does exist and you only want to check if it has any entries you can simply check it's value:
$array = array(); 
if ($array) {
print 'true';
} else {
print 'false'; // prints this one.
}

grandpa

8:08 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can also use is_array, useful if an array is expected, but may not have been set.

if(is_array($array)) {
// do something
}

audiolizard

8:11 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



In both of your examples, you declare the variable $array as type array();
$array = array();

In my case, either the array will be created on the previous page and passed in the header, or it won't. Both situations are legitimate. If my array is named $array as in your examples, won't redeclaring the variable erase the values in the array?

Also, when I use isset(); for my test, and the array exists, the script works fine. When the array doesn't exist, I get the following message (edited for simplicity):

Notice: Undefined variable: array in myFile

audiolizard

8:23 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



if I use is_array(), I run into the same problem:
Notice: Undefined variable: array in pathToMyFile

Here are the exact tests mentioned so far that I've used, among others:

if (!isset($accessories)) {

if (!is_array($accessories)) {

My array is named $accessories in this case, if it is actually created.

Really the only problem I seem to have at this point is having the Notices show up in my page. Everything seems to work otherwise. Is there perhaps another way to eliminate the notices?

coopster

8:43 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, if you declare the variable as shown in the examples you will indeed overwrite any values that may have existed. That was shown merely as an example. If the array already exists you obviously won't be doing this.

Using isset() should not throw a notice error. You must be doing something else in your control logic test. What is the exact statement that is throwing the error?