AmlanRD
AmlanRD

Reputation: 41

Get result as variable from Json output

I'm using a Recharge API. The API receives the response in Json format. I need to store a output value in variable from the Json response in PHP.

// Response received from API url  
$output = '{"data":[{"user":"abcd123","bal":"500","error_code":200,"resText":"Success"}]}';

//Decoding output  
$json = json_decode($output, true);

//Print
print_r($json);

//Store a value as variable  
$bal = $json['bal'];  

Getting error that bal is undefined index.

Please help me with the proper way to get the value of bal in the variable $bal from the API response.

Upvotes: 0

Views: 213

Answers (1)

CollinD
CollinD

Reputation: 7573

Your JSON object actually stores all of the data in an attribute called data, which is in turn an array of objects (just 1 really). So instead try

$json['data'][0]['bal']

Upvotes: 2

Related Questions