php - 检查数组是否不为空

标签 php arrays

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

var_dump: 字符串(0) ""

我试图检查数组的一部分是否不为空,然后显示代码,但代码无论如何都会显示。

我已经尝试过!is_null !empty。我不太确定哪个应该是正确的或者我应该选择: if (sizeof($book['Booking']['comments'])>0)

代码:

<?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
(
[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
    )

最佳答案

我怀疑它可能包含 (bool) FALSE,而 is_null() 则不然。

简单地尝试一下:

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

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

关于php - 检查数组是否不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7807630/

相关文章:

php - 字节到千兆字节的转换不起作用

c++ - 使用可变参数函数将整数和/或整数数组转换为单个int数组

c++ - 如何使用动态调整大小的字符串数组?

java - 从 Arraylist 中分割字符串 (java)

php - 如何从 Laravel Passport 中的 Authorization Code Grant 获取授权码?

php - 在 Postgresql 数据库中处理 NaN

javascript - 通过 javascript 将值传递给 php

javascript - 如何使用javascript在for循环中获取Response json数据

arrays - Scala:array.toList 与 array.to[List]

sql - 如何声明数组的元素不可为空?