简体   繁体   English

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

[英]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: 有一些PHP函数可以完成不同的工作:


isset — Determine if a variable is set and is not NULL isset —确定是否设置了变量并且不为NULL

Example: 例:

if(isset($list_array)){//do something} will do something if $list_array is set to a value other than NULL. if(isset($list_array)){//do something} $ list_array设置为非NULL的值, if(isset($list_array)){//do something}将执行某些操作。

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


empty — Determine whether a variable is empty. empty —确定变量是否为空。 This function returns true if the variable or array is an empty string, false, array(), NULL, 0 or an unset variable. 如果变量或数组为空字符串,false,array(),NULL,0或未设置的变量,则此函数返回true I prefer the use of the empty function. 我更喜欢使用empty函数。

Example: 例:

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

See: http://uk3.php.net/empty 请参阅: http//uk3.php.net/empty


is_null — Identifies whether or not a variable is NULL by returning either true or false. is_null —通过返回true或false来标识变量是否为NULL。 It returns true only when the variable is null. 仅当变量为null时,它才返回true。 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. is_null()isset()相反,不同之处在于isset()也可以应用于未知变量,而is_null()仅可用于声明的变量。

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

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


Let's stick with empty : 让我们坚持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 您的循环不起作用,因为PHP数组是从零开始的,您应该从零开始并在$z < $arr_length继续

I found two errors in your code: 我在您的代码中发现了两个错误:

  1. a parenthesis is not closed in Attempt 1 尝试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=>...) 在您的for循环中,您是从1开始而不是从0开始(因此,您正在考虑该数组将类似于array(1=>..., 2=>..., 3=>...) ,虽然实际上是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. 您可以在这里尝试,只需简单地复制并粘贴即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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