2013-10-15 30 views
0

如何優雅地將以下內容轉換爲JSON,以便「數據」(即1,2,3,4和5)中的每個元素都是分開的,並且易於遍歷而不使用str_replace或preg_replace?使用PHP在JSON中細分數據

{ 
    "status":"success", 
    "data":{ 
     "1":"this is an element", 
     "2":"this is an element", 
     "3":"this is an element", 
     "4":"this is an element", 
     "5":"this is an element", 
    } 
} 
+1

你是什麼意思的「分開和容易遍歷」?它不是JSON嗎? –

+0

已經看起來像JSON,看起來你已經在'data'中有五個單獨的條目。 –

+0

只需'json_decode',str_replace必須對它做什麼? –

回答

4

假設你的意思是你已經有了一個JSON字符串,使用json_decode(),並提供第二個參數作爲true得到解碼JSON的關聯數組。

$str = '{"status":"success","data":{"1":"this is an element","2":"this is an element","3":"this is an element","4":"this is an element","5":"this is an element"}}' 
$json = json_decode($str, true); 

變量$json將是一個關聯數組。