Forum: Building APIs & Wire Protocol

Post: JSON Response to PHP Array

JSON Response to PHP Array
Ghalia
Dec 16, 2014
Hi everybody,
I'm trying a curl request , this is my code


$voltdbserver = "http://apvv-volt-poc:8080/api/1.0/";
$proc = "SELECT2";

$a = array($username,$password);

$params = json_encode($a);
$params = urlencode($params);
$querystring = "Procedure=$proc&Parameters=$params";

$ch= curl_init();

curl_setopt($ch, CURLOPT_URL, $voltdbserver);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

curl_close($ch);

$json = json_decode($result);
//$json = json_decode($result, true);


print_r($json);


The json response I got is:

{"status":1,"appstatus":-128,"statusstring":null,"appstatusstring":null,"results":[{"status":-128,"schema":[{"name":"ID","type":5},{"name":"USER_NAME","type":9},{"name":"DISPLAY_NAME","type":9},{"name":"PASSWORD","type":9},{"name":"EMAIL","type":9},{"name":"ACTIVATION_TOKEN","type":9},{"name":"LAST_ACTIVATION_REQUEST","type":5},{"name":"LOST_PASSWORD_REQUEST","type":5},{"name":"ACTIVE","type":5},{"name":"TITLE","type":9},{"name":"SIGN_UP_STAMP","type":5},{"name":"LAST_SIGN_IN_STAMP","type":5}],"data":[[10,"Ghalia","ghalia","ghalia","ghalia@mail","6",7,8,9,"10",11,12]]}]}


the part I need is just the array "data":[[10,"Ghalia","ghalia","ghalia","ghalia@mail","6",7,8,9,"10",11,12]]
How can I convert it to php array? I tried
 $user = $json['results']['data'];
print_r($user);


but it dosn't work.
nshi
Dec 16, 2014
Because "results" and "data" fields contain arrays, you need to access the data array like this,
print_r($json->results[0]->data[0]);


We have a native PHP extension that is more efficient and easier to use. You can download it from http://voltdb.com/community/monitoring-tools .
Ghalia
Dec 16, 2014
Thank you so much :)
acheter
Dec 4, 2016
Use json_decode, found here: http://php.net/manual/en/function.json-decode.php.

Sample:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));