alias51
alias51

Reputation: 8638

How to select value from JSON array

I am trying to select a variable from an array (at least I think it's stored as an array):

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

gives

"status":200,"response":{

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

}

How can I select the call_duration value (in php)? I've tried $response['call_duration'], which I thought should work but returns nothing?

Upvotes: 0

Views: 3681

Answers (1)

Daniel Howard
Daniel Howard

Reputation: 5178

$response['call_duration'] was very nearly correct, but I think you need:

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

Looking at your output after converting to json, I think the original array, $response, looks like this (in PHP array format)

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

So, you need to go an extra level deep into the array to get call_duration.

Upvotes: 3

Related Questions