开发者

How to find whether a json array is empty or not?

开发者 https://www.devze.com 2023-02-21 06:02 出处:网络
How to find whether a json arra开发者_JAVA百科y is empty or not using PHP? empty($jsonarray) seems doesn\'t work!Assuming you have decoded the JSON, yes it does.

How to find whether a json arra开发者_JAVA百科y is empty or not using PHP? empty($jsonarray) seems doesn't work!


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


Try this

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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消