2014-06-13 112 views
1

Im new to php。我有以下情况:访问数组中的一个元素

$params = array("code" => $_GET["code"], "redirect_uri" => $redirectUrl); 
$response = $client->getAccessToken($accessTokenUrl, "authorization_code", $params); 

$accessTokenResult = $response["result"]; 

$client->setAccessToken($accessTokenResult["access_token"]); 
$client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER); 

$response = $client->fetch("https://oauth.reddit.com/api/v1/me.json"); 

而结果(print_r)从$response这是一个:

Array (
    [result] => Array 
     (
      [name] => histograf 
      [created] => 12869553489 
      [gold_creddits] => 0 
      [created_utc] => 12869553489 
      [link_karma] => 1 
      [comment_karma] => 11 
      [over_18] => 1 
      [is_gold] => 
      [is_mod] => 0 
      [has_verified_email] => 
      [id] => ap11l 
     ) 

    [code] => 200 
    [content_type] => application/json; charset=UTF-8) 

的一点是,现在,我怎么能访问该元素的名称?我需要让用户名在pgm中更进一步。

我已经试过这些,没有工作:

  • $username = $response[0];
  • $username = $response->name;
  • $username = $response[0]name;

回答

1

解决方案:

$username = $response['result']['name']; 
  • $ =输入反应的阵列。
  • ['result'] =你的情况中的第一个元素。
  • ['name'] =访问名称值。
+0

非常感谢你!现在工作! – histograf

0

在你的榜样,从响应得到了[名]字段:

$username = $response['result']['name'];

['name']替换为您尝试访问的任何字段。对于未来的参考,如果你是在一个HTML文件的测试脚本的输出,你可以围绕你的print_r()<pre>标签这样:

<pre><?php print_r($response); ?></pre>

这使得print_r()更可读,你会能够看到阵列的深度。