7

I need to pass php array to jquery for some tasks. The php array is created for SESSION and then json_encoded for jquery. After that I'll store that variable to window js namespace in order to use the array in my jquery script.

if(isset($_SESSION['mySession'])){
    $json_array = json_encode($php_array);
}

<script>
    <?php echo "window.json_from_php = ".$json_array; ?>
</script>

The thing i need to do is to check whether this array exist at all/is empty/undefined before doing anything. The check clause succeeds for 'undefined' operand but not for the rest. How can I check in jquery whether array is empty or not?

This is the output of the json_encoded array after initialization, when there isn't any elements in the $php_array:

string '[""]' (length=4)

And here is the jquery:

$(function() {
        var results = $('#results');
        var js_array = window.json_from_php;
        var count = js_array.length;

        if ((typeof window.json_from_php === 'undefined') || (js_array === null) || (jQuery.isEmptyObject(js_array)) || (count === 0)) {
            $('<p>Array is empty.</p>').appendTo(results);
        }
        else {
            jQuery.each(js_array,function(key,val){
                $('<ul><li id="'+key+'">'+val+'</li></ul>').appendTo(results);                  
            });
        }
    });
art2
  • 425
  • 1
  • 5
  • 18

2 Answers2

19

This is how I check for an empty/null json array.

if (js_array == undefined || js_array == null || js_array.length == 0){
    $('<p>Array is empty.</p>').appendTo(results);
}

If your json array is [""] then you may need to add something like

if (js_array == undefined || js_array == null || js_array.length == 0 || (js_array.length == 1 && js_array[0] == ""))
JoeFletch
  • 3,820
  • 1
  • 27
  • 36
  • 2
    The first two checks are exactly the same. – pimvdb Nov 14 '12 at 16:01
  • I should do it like this: `if (!js_array || !js_array.length){...` – Vithozor Nov 14 '12 at 16:02
  • @Víctor, is `!js_array` the equivalent of `js_array == undefined || js_array == null`? – JoeFletch Nov 14 '12 at 16:03
  • 1
    @JoeFletch Nope. `!js_array` is equivalent to `js_array == null || js_array === '' || js_array === 0 || js_array === -0 || js_array === false || isNaN(js_array)`. – Šime Vidas Nov 14 '12 at 16:04
  • @pimvdb, is `js_array == undefined` equivalent to `typeof js_array === 'undefined'` (I will have to check.) – JoeFletch Nov 14 '12 at 16:05
  • @JoeFletch Nope. `js_array == undefined` is equivalent to `js_array === undefined || js_array === null` (assuming that `undefined` is indeed the `undefined` value, but that's another story) – Šime Vidas Nov 14 '12 at 16:06
  • @Joen Fletch: Not exactly, but my point is that `== undefined` disallows `undefined`/`null` just as `== null` does. – pimvdb Nov 14 '12 at 16:06
  • Ok, thanks all for your feedback. I have learned something here and need to rethink some of my logic null/undefined error checking. Should I leave my answer or delete it? I feel like there are good comments here, but I have to go over some things before I edit my answer. Feedback is appreciated. – JoeFletch Nov 14 '12 at 16:12
  • Please leave your answer (or leave it as edited), since the last operands was a solution for me and the discussion followed is good clarification for the whole issue here. – art2 Nov 14 '12 at 18:44
  • 4
    @JoeFletch you can learn something interesting about truthy and falsy values here http://www.sitepoint.com/javascript-truthy-falsy/ , it's very useful when working with Javascript :) – Vithozor Nov 15 '12 at 07:29
1

This should be enough:

if (typeof js_array === 'undefined' || !js_array.length) {
   $('<p>Array is empty.</p>').appendTo(results);
} else {...}
A. Wolff
  • 74,033
  • 9
  • 94
  • 155