简体   繁体   中英

How to Check if Multi-Dimensional Associative Array is Empty

I have an array as follows:

$list_array = array();

$list_array[] = array (
                 'id' => 1,
                 'name' => 'Sean',
                 'codename' => 'Maverick'
                 );

$list_array[] = array (
                 'id' => 2,
                 'name' => 'Matt',
                 'codename' => 'Diesel'
                 );

$list_array[] = array (
                 'id' => 3,
                 'name' => 'Bonnie',
                 'codename' => 'Princess'
                 );

I am trying to figure out how I can check to see if it's empty. I look on the site and tried a couple of things, but it's not working. Here are the things I've tried.

Attempt 1:
if (empty($list_array)
    echo "Array is empty";


Attempt 2:
$arr_empty = true;
$arr_length = count($list_array);
echo "Length: " . $arr_length . "<br>";
for ($z=1; $z<=$arr_length; $z++)
{
    $arr_length2 = count($list_array[$z]);
    echo $z . " Length2: " . $arr_length2 . "<br>";
    if (empty($list_array[$z]))
        echo $z . " Is Empty<br>";
}

I feel like I'm missing the obivious here.

There are a few PHP functions for this that all do slightly different things:


isset — Determine if a variable is set and is not NULL

Example:

if(isset($list_array)){//do something} will do something if $list_array is set to a value other than NULL.

See: http://uk3.php.net/manual/en/function.isset.php


empty — Determine whether a variable is empty. This function returns true if the variable or array is an empty string, false, array(), NULL, 0 or an unset variable. I prefer the use of the empty function.

Example:

if(!empty($list_array)){
    //do something with array here, such as a foreach or while loop.
}

See: http://uk3.php.net/empty


is_null — Identifies whether or not a variable is NULL by returning either true or false. It returns true only when the variable is null. is_null() is opposite of isset() , except for one difference that isset() can also be applied to unknown variables whereas is_null() can only be used for declared variables.

Example: if(is_null($var)){ //do something }

See: http://uk3.php.net/manual/en/function.is-null.php


Let's stick with empty :

if(empty($list_array)){ $msg = "Array is empty!"; }
if(!isset($msg)){echo $msg;}

empty should work:

if (empty($list_array))
    echo "Array is empty";
else
    echo "Array is not empty";

Your loop does not work because PHP arrays are zero based, you should start with zero and continue while $z < $arr_length

I found two errors in your code:

  1. a parenthesis is not closed in Attempt 1
  2. in your for cycle you are starting from 1 instead of starting from 0 (so, you are considering that the array will be something like array(1=>..., 2=>..., 3=>...) , while it is actually array(0=>..., 1=>..., 2=>...)

So, here is your code, fixed:

$list_array = array();

$list_array[] = array (
                 'id' => 1,
                 'name' => 'Sean',
                 'codename' => 'Maverick'
                 );

$list_array[] = array (
                 'id' => 2,
                 'name' => 'Matt',
                 'codename' => 'Diesel'
                 );

$list_array[] = array (
                 'id' => 3,
                 'name' => 'Bonnie',
                 'codename' => 'Princess'
                 );

//Attempt 1:
if (empty($list_array))
    echo "Array is empty";


//Attempt 2:
$arr_empty = true;
$arr_length = count($list_array);
echo "Length: " . $arr_length . "<br>";
for ($z=0; $z<$arr_length; $z++)
{
    $arr_length2 = count($list_array[$z]);
    echo $z . " Length2: " . $arr_length2 . "<br>";
    if (empty($list_array[$z]))
        echo $z . " Is Empty<br>";
}

You can try it here , with a simple copy and paste.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM