Php 检查数组是否为空
********更新**********Php 检查数组是否为空,php,arrays,Php,Arrays,********更新********** var\u转储:字符串(0)“” 我试图检查数组的一部分是否为空,然后显示代码,但代码仍然显示 我试过!是空的!空的。我不确定哪一个应该是正确的,或者我应该使用: if(sizeof($book['Booking']['comments'])>0) 代码: <?php if (!empty($book['Booking']['comments'])) {?> <table width="100%" border="0">
var\u转储:字符串(0)
“”
我试图检查数组的一部分是否为空,然后显示代码,但代码仍然显示
我试过!是空的!空的
。我不确定哪一个应该是正确的,或者我应该使用:
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
)
if(count($book['Booking']['comments'])>0){…}
我怀疑它可能包含(bool)FALSE
,这对于is_null()
是不正确的
试着简单地说:
if ($book['Booking']['comments']) {
这也适用于任何计算结果为
FALSE
,如空字符串。我认为在这种情况下,$book['Booking']['comments']
甚至不是数组。所以你可以直接使用strlen
您可能希望在测试该属性是否为空()
之前,删除该属性的所有空白
编辑:我假设它是一个字符串。它看起来不像一个空数组。
!空($var)
,计数($var)>0
,$var
,所有这些都将在大多数情况下工作。empty()的“优点”是当给定的变量/数组键不存在时不会抛出通知,但如果您不必担心布尔检查(!$var
)就足够了(请查看哪些类型转换为FALSE)。它也是代码中最短的。这不是一个确定的数组
执行
var_dump($book[“Booking”][“comments”])
以相应地检查数据类型我认为您可以尝试使用以下逻辑,并尝试将其简化一点
<?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;?>
我还没有对此进行测试,因为我现在无法测试,但希望这能对您有所帮助。您的问题太本地化了。您的代码中有一些输入错误或类似的错误 如果(!empty($var))或
如果($var)
,则在您的案例中使用什么绝对没有区别。所以,如果($book['Booking']['comments']){
起作用了,那么你的如果(!empty($book['Booking']['comments'])
也没有问题。所以,这个问题根本没有问题
所有这些试图回答这个非真实问题的答案都是胡说八道
唯一的问题是jotorres1提到的空间符号,但您已经说过没有
$array=array();
reset($array)
返回FALSE
$array=array('foo','bar');
reset($array)
返回第一个元素('foo'
)如果您想确定您正在测试的变量实际上是否显式地是一个非空数组,可以使用如下方法:
if ($book['Booking']['comments'] !== array()) {
//your logic goes here
....................
}
此逻辑比其他解决方案更快。它还将保持编码标准。注释的类型应该是什么?
empty
在所有情况下都可以正常工作,除非字符串完全由空格组成。什么var_dump($book['Booking'][/comments']))
给出了?可能重复的,那么你应该这样做-如果($book[“Booking”][“comments”]==”)我想显示代码,如果它不是空的,我会使用==?谢谢这对空不是真的()
-谁说你的?如果(!$book[“Booking”[“comments”][“comments”])){
会适得其反。哇!你花了两个月才纠正一个说法;)另一个呢?@Col.Shrapnel某人+1随机告诉我,老实说,自从我写了这篇文章后,我就没有再访问过这一页,我承认这不是最好的答案……但它的不足之处也可能是正确的。。。