Alan Alan - 2 months ago 7
PHP Question

How to convert a string into an array

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!

Answer

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'];