0

I am having huge array. Its structure is like this:

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

I trying to check simply if the above array is empty or not.

<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>

Where I am going wrong? Why above all conditions are not working?

Need Help.

Thanks.

12
  • Simply you can check if an array is empty or not using if(empty($arrayName)). Commented Jan 12, 2013 at 5:57
  • 1
    is the above the output of print_r($this->revArr); ?? Commented Jan 12, 2013 at 5:57
  • There was a similar question yesterday. The problem was actually the part of the code posted was in a function and the variable wasn't being passed or global. Because the person didn't post enough code it took them an hour to work out instead of finding an answer in minutes. Please post more relevant code. Commented Jan 12, 2013 at 6:03
  • @popnoodles : ok I'll modify my post. Please wait Commented Jan 12, 2013 at 6:05
  • 1
    If you believe so. Good luck with that. Commented Jan 12, 2013 at 6:31

5 Answers 5

3

Could it be that you just forgot the else? From what I see this code will always output

"No Data Found"

even if data is present. If data is present it should also output the data as requested.

//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>

Are you sure your api returns nothing if it fails to find the data? Most times an api will return False or an error array on failure so testing for empty may not be appropriate.

Good Luck.

Sign up to request clarification or add additional context in comments.

Comments

0

You might try:

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

The count function will return true if the variable is a string, so you need to check that it is an array first.

5 Comments

What does "not working" mean? Is it letting through empty arrays, or is it not letting through populated arrays?
its still going in else condition.
The either $this->revArr is not an array, or it contains no data. There is no other option. What happens when you do a var_dump just before the if statement?
var_dump prints the array
And what happens when you add this just before the if statement: echo gettype($this->revArr);
0

Take a Look: empty

isset

is_array

1 Comment

<?php function array_empty($mixed) { if (is_array($mixed)) { foreach ($mixed as $value) { if (!array_empty($value)) { return false; } } } elseif (!empty($mixed)) { return false; } return true; } ?>
0

okay try these 2 steps

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

it will return NULL if there is no key otherwise returns the first key

Comments

0

You can use this function to check if an array is empty. There is no other easy way.

// 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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.