如何将字符串转换为数组

How to convert a string into an array

所以我得到了一个看起来像这样的字符串:

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

但我只需要访问 "#############"(这是访问令牌),但为了做到这一点,我需要将此字符串转换为数组。 我试过这样:

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

但是通过这样做我得到了这样的东西:

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

知道为什么吗?欢迎任何帮助!感谢您的宝贵时间!

这是一个 json 对象,因此您需要 decode 它。

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

您的字符串看起来像 JSON。您可以在字符串上尝试 json_decode 函数。

$array = json_decode($your_string, true);
echo $array['access_token'];