首页 > 解决方案 > 如何从数组php中只获取一个元素

问题描述

我有这个 JSON 数组数据

{"accessToken":"eyJhbGciOiJSUzUxMiJ9.e","refreshToken":"QErx0bUxyx6wxFj5AXcAh21UuyO8ad/ULIaGlP3LU2lmXGnx0twbYdM+nJyfwAcK9Av50uZ3fSZ/2nhJwIi+bA==","expiresIn":"2021-11-11T10:20:33Z","issuedAt":"2021-11-11T10:05:33Z","tokenType":"Bearer"}

如何accessToken从数组中仅获取对象。IEyJhbGciOiJSUzUxMiJ9.e

这就是我尝试过的方式

$response = curl_exec($curl);

curl_close($curl);
$token = $response['accessToken'];
echo $token;

但我得到这个错误

Warning: Illegal string offset 'accessToken'

标签: phpjson

解决方案


$response = curl_exec($curl);

curl_close($curl);
$data = json_decode($response, true);
echo $data['accessToken'];

推荐阅读