【问题标题】:How do I get only one element from an array php [duplicate]如何从数组php中只获取一个元素[重复]
【发布时间】:2021-12-23 20:04:14
【问题描述】:

我有这个 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 对象。即yJhbGciOiJSUzUxMiJ9.e

这就是我尝试过的方法

$response = curl_exec($curl);

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

但我得到了这个错误

Warning: Illegal string offset 'accessToken'

【问题讨论】:

    标签: php json


    【解决方案1】:

    在尝试访问数组之前尝试一下

    json_decode($response, true);
    

    【讨论】:

      【解决方案2】:
      $response = curl_exec($curl);
      
      curl_close($curl);
      $data = json_decode($response, true);
      echo $data['accessToken'];
      

      【讨论】:

      • 它工作正常,我很感激
      猜你喜欢
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 2011-11-14
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多