Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if array is not empty

Tags:

arrays

php

********Update**********

var_dump: string(0) ""

I am trying to check if part of an array is not empty then display code but the code get's displayed anyway.

I have tried !is_null !empty. I am not really sure which should be correct or should I go with: if (sizeof($book['Booking']['comments'])>0)

Code:

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

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] => [email protected]
        [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
    )
like image 439
Keith Power Avatar asked Oct 18 '11 13:10

Keith Power


2 Answers

I suspect it may contain (bool) FALSE, which is not true for is_null().

Try simply:

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

This should also work for anything that evaluates to FALSE, like an empty string.

like image 152
DaveRandom Avatar answered Sep 28 '22 04:09

DaveRandom


if (count($book['Booking']['comments']) > 0) { ... }

like image 40
Nathan Loding Avatar answered Sep 28 '22 04:09

Nathan Loding