-2

here i have json output as below.what i want do is i want to take scope,production,refreshtoken,access_token as separate php variables from var_dump output.

here is the json output

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"}', )

Here is my 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>";
colombo
  • 520
  • 2
  • 9
  • 24

3 Answers3

1

You can use json_decode and extract:

<?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;

Output:

TARGET
bearer
2324
4567f358c7b203fa6316432ab6ba814
55667dabbf188334908b7c1cb7116d26
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

This isn't too hard at all. You'll just need to json_decode() the present JSON and fetch the fields you require:

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

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

Example/Demo

Darren
  • 13,050
  • 4
  • 41
  • 79
0

Your array:

$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"}');

Just decode the body part of your array.

You got this:

$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
)

Now access this array and get all the value from it.

foreach($arr2 as $key => $value){
    echo $key." => ".$value."<br/>";
}

Result

scope => TARGET
token_type => bearer
expires_in => 2324
refresh_token => 4567f358c7b203fa6316432ab6ba814
access_token => 55667dabbf188334908b7c1cb7116d26
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42