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

7

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
  • 857,932
  • 118
  • 1,152
  • 1,264
5

Try this

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

//or
if(empty(json_decode($jsonarray,1))) {
    echo "empty";
}
Starx
  • 75,098
  • 44
  • 181
  • 258
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.