2013-05-09 63 views
1

我有两个JSON解码字符串:在PHP计算阵列的数量解码的字符串

{"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正是我粘贴到一个文本字段,现在用于测试目的。

回答

4

做的,

json_decode($jsonString, true); 

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

+0

@ chandresh_cool我用下面的代码:$ Json_String =($ _ POST [ '的Json']); $ Json_Decoded = json_decode($ Json_String,true); echo count(json_decode($ Json_String,true));但是,当我粘贴上面的第一个字符串,我得到1,当我粘贴上面的第二个字符串,我仍然得到1 – user2363025 2013-05-09 09:14:55

+0

尝试'sizeof($ Json_Decoded ['Coords']);' – Waygood 2013-05-09 09:15:54

+0

你必须传递每个数组的键像回声计数($ arr ['Coords'])); – 2013-05-09 09:16:16

2

,如果你只是想kown长度试试这个:

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