How to find whether a json array is empty or not using PHP? empty($jsonarray) seems doesn't work!
Asked
Active
Viewed 1.7k times
5
-
Seriously, since when did PHP have json arrays? Never heard of them before. – awm Apr 02 '11 at 07:27
-
1empty(json_decode($jsonarray)) – Gaurav Apr 02 '11 at 07:27
-
Yup, that's most likely what was intended... – awm Apr 02 '11 at 07:35
-
This is useful: https://stackoverflow.com/a/2216063 – ColinWa Apr 24 '17 at 13:58
3 Answers
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.

Szigyártó Mihály
- 521
- 6
- 11