检查数组是否为空

时间:2022-09-06 11:13:06

********Update**********

更新* * * * * * * * * * * * * * * * * *

var_dump: string(0) ""

var_dump:string(0)”“

I am trying to check if part of an array is not empty then display code but the code get's displayed anyway.

我试着检查数组的一部分是不是空的,然后显示代码,但是代码还是显示出来了。

I have tried !is_null !empty. I am not really sure which should be correct or should I go with: if (sizeof($book['Booking']['comments'])>0)

我试过了,is_null,空的。我不确定哪一个应该是正确的,还是应该使用:如果(sizeof($book['Booking']['comments'])>0)

Code:

代码:

<?php if (!empty($book['Booking']['comments'])) {?>
    <table width="100%" border="0">
        <tbody>
            <tr>
                <td style="font-family:'Lucida Grande', sans-serif;font-size:12px;font-weight:normal;color:#666666;">
                    <?=$book['Booking']['comments']?>
                </td>
            </tr>
        </tbody>
    </table>
<? } ?>

Array:

数组:

Array
(
[Booking] => Array
    (
        [id] => 109
        [user_id] => 1
        [corporate_account_id] => 0
        [id_ref] => RES000109
        [price] => 178.00
        [arrival] => 2011-10-18 00:00:00
        [departure] => 2011-10-19 00:00:00
        [rate_title] => 
        [adult_guests] => 4
        [child_guests] => 0
        [company] => gravitate
        [titlename] => 
        [firstname] => John
        [surname] => Doe
        [address1] => 123 Fake St
        [address2] => 
        [city] => New York
        [state] => NY
        [postcode] => 12345
        [country] => US
        [phone] => 1111111111
        [mobile] => 
        [fax] => 
        [email] => example@example.com
        [comments] => 
        [created] => 2011-10-18 13:40:47
        [updated] => 2011-10-18 13:40:47
        [status] => 1
        [cancelled] => 0
        [request_src] => website
        [request_token] => 0
        [token] => ayzrGnx
        [survey_sent] => 0000-00-00 00:00:00
        [survey_returned] => 0000-00-00 00:00:00
        [send_sms] => 0
        [payment_time] => 0000-00-00 00:00:00
        [fullname] =>  John Doe
    )

10 个解决方案

#1


8  

I suspect it may contain (bool) FALSE, which is not true for is_null().

我怀疑它可能包含(bool) FALSE,但is_null()不是这样的。

Try simply:

试一试:

if ($book['Booking']['comments']) {

This should also work for anything that evaluates to FALSE, like an empty string.

这也适用于任何计算结果为FALSE的对象,如空字符串。

#2


3  

your question is too localized. There is some typo in your code or something like that.

你的问题太局限了。你的代码中有些错别字。

There is absolutely no difference what to use in your case, if (!empty($var)) or if ($var). So, if ($book['Booking']['comments']) { worked, there was no problem with your if (!empty($book['Booking']['comments'])) too. So, there was no poin in the question at all.

在您的示例中,使用什么完全没有区别,如果(!empty($var)或if ($var)。所以,如果($book['Booking']['comments']]){有效,那么你的if (!empty($book['Booking']['comments'])也没有问题。所以,这个问题根本就没有问题。

All these answers trying to answer this not-a-real-question are nonsense.

所有这些试图回答这个不现实问题的答案都是无稽之谈。

the only issue can be a space symbol mentioned by jotorres1, but you have already said there are none.

唯一的问题可能是jotorres1提到的空间符号,但您已经说过没有。

#3


2  

if (count($book['Booking']['comments']) > 0) { ... }

如果(count($书(“预订”)(“评论”))> 0){…}

#4


2  

!empty($var), count($var) > 0, !$var, all of these will work in most situations. empty() has the "advantage" of not throwing a notice when the given variable / array key doesn't exist, but if you don't have to worry about that a boolean check (!$var) should suffice (see here which types convert to FALSE). It also happens to be the shortest in code.

!empty($var)、count($var) >、!空()具有在给定变量/数组键不存在时不抛出通知的“优点”,但是如果您不需要担心一个布尔检查($var)就足够了(请参见这里的类型转换为FALSE)。它也是代码中最短的。

#5


1  

I don't think that $book['Booking']['comments'] is even an array in this case. So you could just use strlen http://php.net/manual/en/function.strlen.php

我不认为在这种情况下,$book['预订']['comments']甚至是一个数组。你可以使用strlen http://php.net/manual/en/function.strlen.php

<?php if (strlen($book['Booking']['comments'])) {?>

< ?php如果(strlen($书(“预订”)(“评论”))){ ? >

#6


1  

You may want to trim() that property to remove any whitespace before testing if it is empty().

在测试是否为empty()之前,您可能需要修饰()属性以删除任何空格。

Edit: I'm assuming it is a string. It doesn't look like an empty array.

编辑:我假设它是一个字符串。它看起来不像一个空数组。

#7


1  

that is not an array for sure.

这不是一个确定的数组。

do var_dump($book["Booking"]["comments"]) to check the data type accordingly

var_dump($book["Booking"]["comments"])是否相应地检查数据类型

#8


1  

I think you can try to use this following logic, and try to make it a little bit simpler.

我认为你可以试着用下面的逻辑,试着让它简单一点。

<?php 
    // Only change this
    // and leave everything else as is
    $book_comment = $book['Booking']['comments'];
    $book_comment = trim($book_comment);
    // The reason I use empty trim, is to take 
    // away any space.  
    // In the output, use the original $book['Booking']['comments']

    if(!empty($book_comment)):?>

      <table width="100%" border="0">
          <tbody>
              <tr>
                  <td style="font-family:'Lucida Grande', sans-serif;font-size:12px;font-weight:normal;color:#666666;">
                      <?=$book['Booking']['comments']?>
                  </td>
              </tr>
          </tbody>
       </table>
<?php endif;?>

I have not tested this, as I can't right now, but hopefully that should somewhat help you.

我还没有测试过这个,因为我现在还不能,但是希望这能对你有所帮助。

#9


0  

  1. Empty array:

    空数组:

    $array = array();

    $ =数组数组();

    reset($array) returns FALSE.

    重置(数组)返回FALSE。

  2. Populated array:

    填充数组:

    $array = array('foo', 'bar');

    $ =数组数组(“foo”、“酒吧”);

    reset($array) returns first element ('foo').

    重置($array)返回第一个元素('foo')。

#10


0  

If you want to ascertain whether the variable you are testing is actually explicitly an not empty array, you could use something like this:

如果您想确定您正在测试的变量是否确实是一个非空数组,您可以使用以下方法:

if ($book['Booking']['comments'] !== array()) {
  //your logic goes here
  ....................
}

This logic is more faster from others solution. Also it will maintain coding standard.

这种逻辑比其他解决方案更快。它还将保持编码标准。

#1


8  

I suspect it may contain (bool) FALSE, which is not true for is_null().

我怀疑它可能包含(bool) FALSE,但is_null()不是这样的。

Try simply:

试一试:

if ($book['Booking']['comments']) {

This should also work for anything that evaluates to FALSE, like an empty string.

这也适用于任何计算结果为FALSE的对象,如空字符串。

#2


3  

your question is too localized. There is some typo in your code or something like that.

你的问题太局限了。你的代码中有些错别字。

There is absolutely no difference what to use in your case, if (!empty($var)) or if ($var). So, if ($book['Booking']['comments']) { worked, there was no problem with your if (!empty($book['Booking']['comments'])) too. So, there was no poin in the question at all.

在您的示例中,使用什么完全没有区别,如果(!empty($var)或if ($var)。所以,如果($book['Booking']['comments']]){有效,那么你的if (!empty($book['Booking']['comments'])也没有问题。所以,这个问题根本就没有问题。

All these answers trying to answer this not-a-real-question are nonsense.

所有这些试图回答这个不现实问题的答案都是无稽之谈。

the only issue can be a space symbol mentioned by jotorres1, but you have already said there are none.

唯一的问题可能是jotorres1提到的空间符号,但您已经说过没有。

#3


2  

if (count($book['Booking']['comments']) > 0) { ... }

如果(count($书(“预订”)(“评论”))> 0){…}

#4


2  

!empty($var), count($var) > 0, !$var, all of these will work in most situations. empty() has the "advantage" of not throwing a notice when the given variable / array key doesn't exist, but if you don't have to worry about that a boolean check (!$var) should suffice (see here which types convert to FALSE). It also happens to be the shortest in code.

!empty($var)、count($var) >、!空()具有在给定变量/数组键不存在时不抛出通知的“优点”,但是如果您不需要担心一个布尔检查($var)就足够了(请参见这里的类型转换为FALSE)。它也是代码中最短的。

#5


1  

I don't think that $book['Booking']['comments'] is even an array in this case. So you could just use strlen http://php.net/manual/en/function.strlen.php

我不认为在这种情况下,$book['预订']['comments']甚至是一个数组。你可以使用strlen http://php.net/manual/en/function.strlen.php

<?php if (strlen($book['Booking']['comments'])) {?>

< ?php如果(strlen($书(“预订”)(“评论”))){ ? >

#6


1  

You may want to trim() that property to remove any whitespace before testing if it is empty().

在测试是否为empty()之前,您可能需要修饰()属性以删除任何空格。

Edit: I'm assuming it is a string. It doesn't look like an empty array.

编辑:我假设它是一个字符串。它看起来不像一个空数组。

#7


1  

that is not an array for sure.

这不是一个确定的数组。

do var_dump($book["Booking"]["comments"]) to check the data type accordingly

var_dump($book["Booking"]["comments"])是否相应地检查数据类型

#8


1  

I think you can try to use this following logic, and try to make it a little bit simpler.

我认为你可以试着用下面的逻辑,试着让它简单一点。

<?php 
    // Only change this
    // and leave everything else as is
    $book_comment = $book['Booking']['comments'];
    $book_comment = trim($book_comment);
    // The reason I use empty trim, is to take 
    // away any space.  
    // In the output, use the original $book['Booking']['comments']

    if(!empty($book_comment)):?>

      <table width="100%" border="0">
          <tbody>
              <tr>
                  <td style="font-family:'Lucida Grande', sans-serif;font-size:12px;font-weight:normal;color:#666666;">
                      <?=$book['Booking']['comments']?>
                  </td>
              </tr>
          </tbody>
       </table>
<?php endif;?>

I have not tested this, as I can't right now, but hopefully that should somewhat help you.

我还没有测试过这个,因为我现在还不能,但是希望这能对你有所帮助。

#9


0  

  1. Empty array:

    空数组:

    $array = array();

    $ =数组数组();

    reset($array) returns FALSE.

    重置(数组)返回FALSE。

  2. Populated array:

    填充数组:

    $array = array('foo', 'bar');

    $ =数组数组(“foo”、“酒吧”);

    reset($array) returns first element ('foo').

    重置($array)返回第一个元素('foo')。

#10


0  

If you want to ascertain whether the variable you are testing is actually explicitly an not empty array, you could use something like this:

如果您想确定您正在测试的变量是否确实是一个非空数组,您可以使用以下方法:

if ($book['Booking']['comments'] !== array()) {
  //your logic goes here
  ....................
}

This logic is more faster from others solution. Also it will maintain coding standard.

这种逻辑比其他解决方案更快。它还将保持编码标准。