dongnaizao8039
2014-05-28 07:53
浏览 23
已采纳

如何检查多维关联数组是否为空

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.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douhu7807 2014-05-28 08:00
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshuo1257 2014-05-28 08:14

    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;}
    
    评论
    解决 无用
    打赏 举报
  • dongzong3053 2014-05-28 08:25

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题