Reputation: 15
I am trying to the balance in a PHP object, I tried everything but am getting array to string conversion error, here is the string
{
"status": true,
"message": "Balances retrieved",
"data": [
{
"currency": "NGN",
"balance": 0
}
]
}
its a response from a remote server, i am trying to get the balance, i tried
$response->data->balance
json_decode($response)->data->balance
json_decode($response[0])->data->balance
but none of them worked, the response from the remote server is stored in a variable called $response
How can i get the balance from the response
Upvotes: 0
Views: 46
Reputation: 5
Gerard's answer is right.
Also you can naviguate the result of a json_decode function in array mode.
<?php
$json = '{
"status": true,
"message": "Balances retrieved",
"data": [
{
"currency": "NGN",
"balance": 0
}
]
}';
$response = json_decode($json);
echo $response["data"][0]["balance"];
echo $response->data[0]->balance;
Using a variable to register the decode answer will save you time.
Upvotes: 0
Reputation: 706
Take a look at this example, that I created: http://sandbox.onlinephpfunctions.com/code/414f23b1a09221ed5bd7f299f5eb73a2f9f932b1
First you decode the JSON string Then access data array at the index 0, so data[0] And then data[0]->balance, access object property 'balance'
Upvotes: 1