How to determine if json array is empty or not?

How to determine if a json array is empty or not using PHP? empty ($ jsonarray) doesn't seem to work!

+5
source share
3 answers

Assuming you decrypted JSON, yes, yes.

<?php
    $json = '{"hello": ["world"], "goodbye": []}';
    $decoded = json_decode($json);
    print "Is hello empty? " . empty($decoded->{'hello'});
    print "\n";
    print "Is goodbye empty? " . empty($decoded->{'world'});
    print "\n";
?>

gives:

Hi, is it empty?
Goodbye is empty? 1

+7
source

try it

if(count(json_decode($jsonarray,1))==0) {
    echo "empty";
}

//or
if(empty(json_decode($jsonarray,1))) {
    echo "empty";
}
+5
source

The empty value of the JSON array is just [], so you can search for it after the name of the array or string if you print the array.

0
source

All Articles