0

I am performing an insertion of values ​​using an api. then, I send the values ​​correctly and get the return on the same page. Eg:

<?php
...
$return= $set->createCharge([
   'description'    => 'Strings etc...',
   'amount'     => 10.0
)];

print_r($return);

?>

This print_r Result is:

stdClass Object
(
[data] => stdClass Object
    (
        [charges] => Array
            (
                [0] => stdClass Object
                    (
                        [code] => 555
                        [reference] => 66
                        [dueDate] => 23/11/2020
                        [checkoutUrl] => https://url/number_return
                        [link] => https://url_return
                        [installmentLink] => https://url
                        [payNumber] => 9999999999
                        [billetDetails] => stdClass Object
                            (
                                [bankAccount] => 888888
                                [ourNumber] => 12121-9
                                [barcodeNumber] => 0541454141411141414141414
                                [portfolio] => 0001
                            )

                    )

            )

    )

[success] => 1

)

I tried to get the value '[code'] using the following:

echo $return[0]['code'];//Dont work

echo $return['data']['charges'][0]['code'];//Dont Work

how can I get the value of [code] or another that is in that array?

1 Answer 1

1

Each element in the "charges" array is an object. A different syntax is used to refer to the members of the object:

//for an array
echo $someArray['someValue'];

//for an object
echo $someObject->someValue;
echo $someObject->getSomeValue()

Therefore, this is the way for you:

echo $return->data->charges[0]->code;

Or, step by step:

var_dump($return);
var_dump($return->data);
var_dump($return->data->charges);
var_dump($return->data->charges[0]);
var_dump($return->data->charges[0]->code);
var_dump($return->data->charges[0]->code->billetDetails);
var_dump($return->data->charges[0]->code->billetDetails->bankAccount);

Sign up to request clarification or add additional context in comments.

3 Comments

HTTP ERROR 500 In Apache error log say : PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array
I updated my answer. Try the code from the last paragraph ("Or, step by step").
Yep. Its work! var_dump($return->data->charges[0]->code); This result -> path_url/code.php:34:int 136023929 Running echo $return->data->charges[0]->code; Output: 136023929 Thanks man.

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.