Alan
Alan

Reputation: 213

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!

Upvotes: 1

Views: 65

Answers (2)

FrankSunnyman
FrankSunnyman

Reputation: 241

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

Upvotes: 2

aynber
aynber

Reputation: 23001

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

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

Upvotes: 1

Related Questions