0

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?

5
  • That's a JSON object. Commented Feb 6, 2014 at 2:03
  • I don't know about php, but if the response has JSON in it, I could do something like var object = JSON.parse(response) and access your property like object.call_duration Commented Feb 6, 2014 at 2:06
  • 1
    You guys are looking too far into this. He already had a php array called $response. Then converted it to json and put it in a file to examine it. The question asks how to get the value of call_duraton, which is trivial: $response['response']['call_duration'] Commented Feb 6, 2014 at 2:09
  • What happens when you do print_r($response). That is usually the cleanest way to find out how to access elements from the structure. Commented Feb 6, 2014 at 2:10
  • @DanielHoward yes - I realized this before you posted your comment. You have it right, I think. Commented Feb 6, 2014 at 2:11

1 Answer 1

3

$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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.