【问题标题】:counting the number of arrays in a php decoded string计算php解码字符串中的数组数量
【发布时间】:2013-05-03 17:12:42
【问题描述】:

我有两个 json 解码字符串:

{"Coords":[{"Accuracy":"66","Latitude":"88","Longitude":"99","Timestamp":"100"}]} 

我还有另一个字符串

 {"Coords":[{"Accuracy":"222","Latitude":"333","Longitude":"444","Timestamp":"2013"},{"Accuracy":"3434","Latitude":"565","Longitude":"676","Timestamp":"7878"}]}.

有没有办法可以回显第一个字符串有 1 个数组,而第二个字符串有 2 个?我的方法如下:

$Json_String=($_POST['Json']);                                                               $Json_Decoded= json_decode($Json_String, true);                                            echo count(json_decode($Json_String, true));                              

其中 $Json_String 就是我现在粘贴到文本字段中以进行测试的内容。

【问题讨论】:

    标签: php arrays count decode


    【解决方案1】:

    做,

    json_decode($jsonString, true);
    

    对于将返回数组的每个字符串。然后您可以轻松计算数组的长度。

    【讨论】:

    • @chandresh_cool 我使用了以下代码:$Json_String=($_POST['Json']); $Json_Decoded= json_decode($Json_String, true);回声计数(json_decode($Json_String,true));但是当我粘贴上面的第一个字符串时,我得到 1,当我粘贴上面的第二个字符串时,我仍然得到 1
    • 您必须传递每个数组的键,例如 echo count($arr['Coords']));
    • @user2363025 试试我说的 echo count($arr['Coords']));
    【解决方案2】:

    如果你只想知道长度试试这个:

        echo substr_count (' {"Coords":[{"Accuracy":"222","Latitude":"333","Longitude":"444","Timestamp":"2013"},{"Accuracy":"3434","Latitude":"565","Longitude":"676","Timestamp":"7878"}]}.','Accuracy');
    

    【讨论】:

      猜你喜欢
      • 2016-05-24
      • 1970-01-01
      • 2016-10-07
      • 2023-03-13
      • 2017-10-20
      • 1970-01-01
      • 2018-04-20
      • 2021-09-28
      • 2020-03-12
      相关资源
      最近更新 更多