2016-05-06 41 views
-2

這裏我有json輸出如下。我想要做什麼是我想要範圍,生產,refreshtoken,access_token作爲獨立的PHP變量從var_dump輸出。如何將vardump輸出的值賦給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

使用json_decode($改編[ '身體'],真正); – JYoThI

回答

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 
0

這不是太難的。你只需要json_decode()JSON,把你需要的字段:

$data = json_decode($json['body']); 

$scope = $data->scope; 
//....etc 

Example/Demo

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