这里我有如下json输出。我想做的是,我想将scope、production、refreshtoken和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>";
您可以使用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
这一点都不难。您只需要json_decode()
当前的JSON
并获取所需的字段:
$data = json_decode($json['body']);
$scope = $data->scope;
//....etc
示例/演示
您的数组:
$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