5

How to find whether a json array is empty or not using PHP? empty($jsonarray) seems doesn't work!

hbase_user
  • 529
  • 4
  • 9
  • 16

3 Answers3

8

Assuming you have decoded the JSON, yes it does.

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

Is hello empty?
Is goodbye empty? 1

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
5

Try this

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

//or
if(empty(json_decode($jsonarray,1))) {
    echo "empty";
}
Starx
  • 77,474
  • 47
  • 185
  • 261
0

The empty JSON array's value is simply [], so you can search for it after the name of the array or in the string if you prints out an array.