繁体   English   中英

如何将字符串转换为数组

[英]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。 您可以在字符串上尝试json_decode函数。

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

这是一个json对象,因此您需要对其进行解码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM