1

So I have obtained a string that looks like this:

string(138) "{"access_token":"#############","token_type":"Bearer","expires_in":3600}"

But I need to access only the "#############" (which is the access token) but in order to do that I need to convert this string to an array. I have tried like this:

//this is the string
$access = $tokenNew["extra_details"];
//here I convert it to an array
$access_token = explode(' ', $access);

But by doing that I get something like this:

array(1) {
   [0] => string(138) "{"access_token ":"##########","token_type ":"Bearer ","expires_in ":3600}"
}

Any ideas why? Any help is welcomed! Thank you for your time!

0

2 Answers 2

2

Your string looks like a JSON. You could try the json_decode function on your string.

$array = json_decode($your_string, true);
echo $array['access_token'];
Sign up to request clarification or add additional context in comments.

2 Comments

Info: The second parameter of json_decode(), forces the returned object to be converted into a associative array.
Thank you it is correct and it helped me understand how json_decode works! Thank you for your time!
1

It's a json object, so you need to decode it.

$json = json_decode($tokenNew["extra_details"], true);
$access_token = $json['access_token'];

1 Comment

Thank you this was the solution! Thank you for your time!

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.