0

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.

1
  • 1
    print_r($json); and following the structure. Commented Feb 9, 2017 at 21:54

1 Answer 1

2

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.