我有巨大的阵容。它的结构是这样的:
Array
(
[0] => Array
(
[id] => 57060
[customer] => 1
[desc] => Customer Object
(
[id] => 51716
[name] => abc xyz
[supplier] => stdClass Object
(
[@size] => 1
[Supplier] => stdClass Object
(
[@chainCode] => EP
[@id] => 13
)
)
[Types] => stdClass Object
(
[@size] => 9
[Type] => Array
(
[0] => stdClass Object
(
[@Code] => 394431
[@TypeId] => 497374
(
[@size] => 27
[Amenity] => Array
(
[0] => stdClass Object
(
[@amenityId] => 8149624
[amenity] => Air Conditioning
)
[1] => stdClass Object
(
[@amenityId] => 8149701
[amenity] =>
)
)
.....
..... and so on
我试图检查上面的数组是否为空。
<table>
<tr>
<td><b>View my Reviews</b></td>
</tr>
<tr>
<td colspan=10>
<div></div>
</td>
</tr>
<?php
if (!empty($this->revArr)) {
foreach ($this->revArr as $review):
?>
<tr>
<td class="col1">
<?php echo ($review['name'] == '') ? 'a guest' : $review['name']; ?> <br/>on <span style="font-size:11px;"><?php echo date("M j, Y", strtotime($review['created_at'])); ?></span>
</td>
<td class="col2">
<span class="name"><a href="/hotelreview/<?php echo $review['hotelId']; ?>"><?php echo $review['Info']->Name; ?></a></span>
<br/>
<span class="address"><?php
echo $review['Info']->address1;
if ($review['Info']->address2 != '') {
echo ' ' . $review['Info']->address2;
}
if ($review['Info']->address3 != '') {
echo ' ' . $review['hotelInfo']->address3;
}
?></span>
<div style="margin-top:10px;">
<h4><b><?php echo ($review['title'] == '') ? 'A Review By' : $review['title']; ?></b></h4>
<p><?php
$detail = str_replace("\r", "\n", $review['detail']);
$detail = preg_replace("#\n+#", "\n", $detail);
echo str_replace("\n", "</p><p>", $detail);
?></p>
</div>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan=10 style="text-align:right;">
<?php echo $pagination; ?>
</td>
</tr>
<?php } {
?>
<tr style="text-align: center;">
<td>No Review Found !!</td>
</tr>
<?php }
?>
</table>
我哪里错了?为什么以上所有条件都不起作用?
需要帮助。
感谢。
答案 0 :(得分:3)
难道你忘了else
吗?从我看到的代码将始终输出
“找不到数据”
即使数据存在。如果存在数据,它还应该按要求输出数据。
//snippet
<?php endforeach; ?>
<tr>
<td colspan=10 style="text-align:right;">
<?php echo $pagination; ?>
</td>
</tr>
<?php } else {//there should be an else here
?>
<tr style="text-align: center;">
<td>No Review Found !!</td>
</tr>
<?php }
?>
</table>
如果找不到数据,你确定你的api返回没有吗?大多数情况下,api会在失败时返回False
或error array
,因此对empty
的测试可能不合适。
祝你好运。
答案 1 :(得分:0)
您可以尝试:
if(is_array($this->revArr) && count($this->revArr)){
//code for displaying array information
}else{
echo "No data found";
}
如果变量是字符串,count
函数将返回true,因此您需要先检查它是否为数组。
答案 2 :(得分:0)
答案 3 :(得分:0)
好的尝试这两个步骤
reset($array_name)
key = key($array_name)
如果没有键则返回NULL,否则返回第一个键
答案 4 :(得分:0)
您可以使用此功能检查数组是否为空。没有其他简单的方法。
// Checks if an array is empty by values recursively.
// If check_all_elements is true, all the elements will be required to be not empty.
function is_array_empty($array, $check_all_elements = false)
{
if (!is_array($array) || empty($array))
return true;
$elements = count($array);
foreach ($array as $element)
{
if (empty($element) || (is_array($element) && is_array_empty($element, $check_all_elements)))
{
if ($check_all_elements)
return true;
else $elements--;
}
}
return empty($elements);
}