【问题标题】:How to find whether a json array is empty or not?如何查找json数组是否为空?
【发布时间】:2011-04-02 07:25:27
【问题描述】:

如何使用 PHP 查找 json 数组是否为空? empty($jsonarray) 似乎不起作用!

【问题讨论】:

  • 说真的,PHP什么时候有json数组了?以前从未听说过。
  • 空(json_decode($jsonarray))
  • 是的,这很可能是本意...
  • 非常感谢您的回复 :-)

标签: php arrays json


【解决方案1】:

假设您已经解码了 JSON,是的。

<?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";
?>

给予:

你好是空的吗?
再见是空的吗? 1

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      【解决方案3】:

      空的JSON数组的值就是[],所以可以在数组名后查找,如果打印出数组,可以在字符串中查找。

      【讨论】:

        猜你喜欢
        • 2021-04-10
        • 2011-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-27
        • 2012-11-03
        相关资源
        最近更新 更多