How to find whether a json array is empty or not using PHP? empty($jsonarray) seems doesn't work!
Asked
Active
Viewed 1.6k 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
-
Thanks a lot for the replies :-) – hbase_user Apr 04 '11 at 07:51
-
This is useful: https://stackoverflow.com/a/2216063 – ColinWa Apr 24 '17 at 13:58
3 Answers
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.
Szigyártó Mihály
- 512
- 7
- 10