这里我有json输出如下所示。我想做的是我想将范围,生产,刷新,access_token作为var_dump输出中的单独php变量。
这是json输出
array ( 'status' => 'OK', 'statusCode' => 200, 'time' => 1.628268, 'header' => array ( 0 => 'HTTP/1.1 200 OK Date: Fri, 06 May 2016 06:22:42 GMT Server: O2-PassThrough-HTTP Content-Type: application/json Pragma: no-cache Cache-Control: no-store Transfer-Encoding: chunked ', ), 'body' => '{"scope":"TARGET","token_type":"bearer","expires_in":2324,"refresh_token":"4567f358c7b203fa6316432ab6ba814","access_token":"55667dabbf188334908b7c1cb7116d26"}', )
这是我的php
var_dump($r);
echo $var[0]."<br>";
echo $var[1]."<br>";
echo $var[2]."<br>";
echo $var[3]."<br>";
echo $var[4]."<br>";
echo $var[5]."<br>";
echo $var[6]."<br>";
echo $var[7]."<br>";
echo $var[8]."<br>";
答案 0 :(得分:1)
您可以使用json_decode并解压缩:
<?php
$a = array ( 'status' => 'OK', 'statusCode' => 200, 'time' => 1.628268, 'header' => array ( 0 => 'HTTP/1.1 200 OK Date: Fri, 06 May 2016 06:22:42 GMT Server: O2-PassThrough-HTTP Content-Type: application/json Pragma: no-cache Cache-Control: no-store Transfer-Encoding: chunked ', ), 'body' => '{"scope":"TARGET","token_type":"bearer","expires_in":2324,"refresh_token":"4567f358c7b203fa6316432ab6ba814","access_token":"55667dabbf188334908b7c1cb7116d26"}', );
$body = json_decode($a['body'], TRUE);
extract($body); //Extracts array keys and converts to variables
echo $scope;
echo $token_type;
echo $expires_in;
echo $refresh_token;
echo $access_token;
输出:
TARGET
bearer
2324
4567f358c7b203fa6316432ab6ba814
55667dabbf188334908b7c1cb7116d26
答案 1 :(得分:0)
这根本不太难。您只需要json_decode()
当前JSON
并获取您需要的字段:
$data = json_decode($json['body']);
$scope = $data->scope;
//....etc
答案 2 :(得分:0)
你的阵列:
$arr = array ( 'status' => 'OK', 'statusCode' => 200, 'time' => 1.628268, 'header' => array ( 0 => 'HTTP/1.1 200 OK Date: Fri, 06 May 2016 06:22:42 GMT Server: O2-PassThrough-HTTP Content-Type: application/json Pragma: no-cache Cache-Control: no-store Transfer-Encoding: chunked ', ), 'body' => '{"scope":"TARGET","token_type":"bearer","expires_in":2324,"refresh_token":"4567f358c7b203fa6316432ab6ba814","access_token":"55667dabbf188334908b7c1cb7116d26"}');
只需解码数组的正文部分。
你得到了这个:
$json = $arr['body'];
$arr2 = json_decode($json);
print_r($arr2);
stdClass Object
(
[scope] => TARGET
[token_type] => bearer
[expires_in] => 2324
[refresh_token] => 4567f358c7b203fa6316432ab6ba814
[access_token] => 55667dabbf188334908b7c1cb7116d26
)
现在访问此数组并从中获取所有值。
foreach($arr2 as $key => $value){
echo $key." => ".$value."<br/>";
}
<强>结果强>
scope => TARGET
token_type => bearer
expires_in => 2324
refresh_token => 4567f358c7b203fa6316432ab6ba814
access_token => 55667dabbf188334908b7c1cb7116d26