0

我有巨大的阵列。它的结构是这样的:

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>

我哪里错了?为什么最重要的条件不起作用?

需要帮忙。

谢谢。

4

5 回答 5

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 会返回Falseerror array失败,因此测试empty可能不合适。

祝你好运。

于 2013-01-12T08:10:46.633 回答
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);
}
于 2014-05-13T14:54:49.510 回答
0

你可以试试:

if(is_array($this->revArr) && count($this->revArr)){
  //code for displaying array information
}else{
   echo "No data found";
}

如果变量是字符串,该count函数将返回 true,因此您需要先检查它是否为数组。

于 2013-01-12T06:03:06.720 回答
0

好的,试试这两个步骤

reset($array_name)
key = key($array_name)

如果没有键,它将返回 NULL 否则返回第一个键

于 2013-01-12T07:42:20.957 回答
0

看一看:

伊塞特

is_array

于 2013-01-12T06:08:59.750 回答