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);                  
            });
        }
    });
5
  • 3
    [""] is not an empty array. It's an array with one element: an empty string. empty array = []. Commented Nov 14, 2012 at 15:59
  • string '[""]' (length=4) What does this line mean? What kind of output is this? It's not a JSON structure. Commented Nov 14, 2012 at 16:01
  • Yeah. I don't get it either. Commented Nov 14, 2012 at 16:02
  • I see it too. Seems like my php array isn't empty in the first place, but instead the first value is just "". Now, instead of checking if it's empty, I need to check if it's "". Commented Nov 14, 2012 at 18:36
  • And the js_array.length/var count in this case is 1 Commented Nov 14, 2012 at 18:48

2 Answers 2

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] == ""))
Sign up to request clarification or add additional context in comments.

10 Comments

The first two checks are exactly the same.
I should do it like this: if (!js_array || !js_array.length){...
@Víctor, is !js_array the equivalent of js_array == undefined || js_array == null?
@JoeFletch Nope. !js_array is equivalent to js_array == null || js_array === '' || js_array === 0 || js_array === -0 || js_array === false || isNaN(js_array).
@JoeFletch you can learn something interesting about truthy and falsy values here sitepoint.com/javascript-truthy-falsy , it's very useful when working with Javascript :)
|
1

This should be enough:

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

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.