2014-02-06 80 views
0

我想選擇一個數組變量(至少我認爲它是存儲爲一個數組):如何從JSON數組中選擇值

$data = json_encode($response); 
file_put_contents('file.txt', $data); 

"status":200,"response":{ 

"api_id":"0f42d6be-8ed2-11e3-822e-22135", 
"bill_duration":36, 
"call_duration":36, 
"total_rate":"0.00300"} 

} 

哪有我選擇call_duration值(在PHP中)?我試過$response['call_duration'],我認爲應該工作,但什麼都不返回?

+0

這是一個JSON對象。 – jeremyjjbrown

+0

我不知道php,但如果響應中有JSON,我可以做一些類似var object = JSON.parse(response)的東西,並訪問你的屬性object.call_duration – KrishnaDhungana

+1

你們看起來太過分了。他已經有一個名爲$ response的php數組。然後將其轉換爲json並將其放入一個文件中進行檢查。這個問題問如何獲得call_duraton的價值,這是微不足道的:$ response ['response'] ['call_duration'] –

回答

2

$response['call_duration']非常接近正確的,但我認爲你需要:

$response['response']['call_duration']

轉換成JSON後,在你的輸出看,我覺得原來的陣列,$response,看起來像這樣(在PHP陣列格式)

$response = array(
    'status'=>200, 
    'response'=>array(
    'api_id'=>'0f....etc', 
    'bill_duration'=>36, 
    ... etc 
) 
); 

所以,你需要深入額外的水平在數組中獲得call_duration