0

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

3 Answers 3

3

Data is an array

json_decode($response)->data[0]->balance
Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

0

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.

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.