2013-05-09 17 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');