Access values within an array

Asked

Viewed 464 times

0

I’m having a question to access the variable inside an array, I’m a bit new in the PHP world (only next to the server without HTML) and I need to handle a server response.

I’m not getting basic access to variable

If I try

echo $data[0]->id

returns

Trying to get property of non-object

The print_r second response array

Array
(
    [0] => Array
        (
            [id] => hook_98kmOL1sMfnZqoPL
            [url] => https://70cfee17.ngrok.io/notifications/payment
            [account] => Array
                (
                    [id] => acc_jZkdN857et650oNv
                    [name] => Lojinha
                )

            [event] => order.paid
            [status] => failed
            [attempts] => 3/3
            [last_attempt] => 2017-03-24T21:32:07Z
            [created_at] => 2017-03-24T21:21:59Z
            [response_status] => 404
            [response_raw] => Tunnel 70cfee17.ngrok.io not found
            [data] => Array
                (
                    [id] => or_e36l73IwgF7MlJwx
                    [code] => ER30H4MU4N
                    [amount] => 199
                    [currency] => BRL
                    [closed] => 1
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [id] => oi_NqW9Rb2LTbsqRa3n
                                    [description] => FLIP Payment
                                    [amount] => 199
                                    [quantity] => 1
                                    [status] => active
                                    [created_at] => 2017-03-24T21:21:58Z
                                    [updated_at] => 2017-03-24T21:21:58Z
                                )

                        )

                    [customer] => Array
                        (
                            [id] => cus_le6ZqXjiBSEOWdoA
                            [name] => Matheus Moreira
                            [email] => [email protected]
                            [delinquent] => 
                            [created_at] => 2016-11-25T23:45:04Z
                            [updated_at] => 2016-12-26T21:34:45Z
                        )

                    [status] => paid
                    [created_at] => 2017-03-24T21:21:58Z
                    [updated_at] => 2017-03-24T21:21:59Z
                    [closed_at] => 2017-03-24T21:21:59Z
                    [charge] => Array
                        (
                            [id] => ch_56YNm9akt4i6mKQ8
                            [code] => ER30H4MU4N
                            [gateway_id] => b21c20f8-21e1-4f56-ac1c-34425c9e9dc9
                            [amount] => 199
                            [status] => paid
                            [currency] => BRL
                            [payment_method] => credit_card
                            [due_at] => 2017-03-24T00:00:00Z
                            [paid_at] => 2017-03-24T21:21:59Z
                            [created_at] => 2017-03-24T21:21:58Z
                            [updated_at] => 2017-03-24T21:21:58Z
                            [customer] => Array
                                (
                                    [id] => cus_le6ZqXjiBSEOWdoA
                                    [name] => Matheus Moreira
                                    [email] => [email protected]
                                    [delinquent] => 
                                    [created_at] => 2016-11-25T23:45:04Z
                                    [updated_at] => 2016-12-26T21:34:45Z
                                )

                            [last_transaction] => Array
                                (
                                    [id] => tran_MpnYkMXhOBTb7Ze8
                                    [transaction_type] => credit_card
                                    [gateway_id] => 9ac7ee2c-02a1-4b19-9f7e-168739a0427b
                                    [amount] => 199
                                    [status] => captured
                                    [success] => 1
                                    [installments] => 1
                                    [acquirer_name] => simulator
                                    [acquirer_affiliation_code] => MUNDI
                                    [acquirer_tid] => 266699
                                    [acquirer_nsu] => 848780
                                    [acquirer_auth_code] => MUNDI
                                    [acquirer_message] => Simulator|Transação de simulação autorizada com sucesso
                                    [acquirer_return_code] => 0
                                    [operation_type] => auth_and_capture
                                    [credit_card] => Array
                                        (
                                            [id] => card_yB3ma9rS8HW7mnA9
                                            [last_four_digits] => 8229
                                            [brand] => Amex
                                            [holder_name] => Tony Stark
                                            [exp_month] => 1
                                            [exp_year] => 2018
                                            [status] => active
                                            [created_at] => 2016-11-29T21:14:07Z
                                            [updated_at] => 2016-11-29T21:14:07Z
                                            [billing_address] => Array
                                                (
                                                    [street] => Malibu Point
                                                    [number] => 10880
                                                    [zip_code] => 90265
                                                    [neighborhood] => Central Malibu
                                                    [city] => Malibu
                                                    [state] => CA
                                                    [country] => US
                                                )

                                        )

                                    [created_at] => 2017-03-24T21:21:58Z
                                    [updated_at] => 2017-03-24T21:21:58Z
                                )

                        )

                    [charges] => Array
                        (
                            [0] => Array
                                (
                                    [id] => ch_56YNm9akt4i6mKQ8
                                    [code] => ER30H4MU4N
                                    [gateway_id] => b21c20f8-21e1-4f56-ac1c-34425c9e9dc9
                                    [amount] => 199
                                    [status] => paid
                                    [currency] => BRL
                                    [payment_method] => credit_card
                                    [due_at] => 2017-03-24T00:00:00Z
                                    [paid_at] => 2017-03-24T21:21:59Z
                                    [created_at] => 2017-03-24T21:21:58Z
                                    [updated_at] => 2017-03-24T21:21:58Z
                                    [customer] => Array
                                        (
                                            [id] => cus_le6ZqXjiBSEOWdoA
                                            [name] => Matheus Moreira
                                            [email] => [email protected]
                                            [delinquent] => 
                                            [created_at] => 2016-11-25T23:45:04Z
                                            [updated_at] => 2016-12-26T21:34:45Z
                                        )

                                    [last_transaction] => Array
                                        (
                                            [id] => tran_MpnYkMXhOBTb7Ze8
                                            [transaction_type] => credit_card
                                            [gateway_id] => 9ac7ee2c-02a1-4b19-9f7e-168739a0427b
                                            [amount] => 199
                                            [status] => captured
                                            [success] => 1
                                            [installments] => 1
                                            [acquirer_name] => simulator
                                            [acquirer_affiliation_code] => MUNDI
                                            [acquirer_tid] => 266699
                                            [acquirer_nsu] => 848780
                                            [acquirer_auth_code] => MUNDI
                                            [acquirer_message] => Simulator|Transação de simulação autorizada com sucesso
                                            [acquirer_return_code] => 0
                                            [operation_type] => auth_and_capture
                                            [credit_card] => Array
                                                (
                                                    [id] => card_yB3ma9rS8HW7mnA9
                                                    [first_six_digits] => 342793
                                                    [last_four_digits] => 8229
                                                    [brand] => Amex
                                                    [holder_name] => Tony Stark
                                                    [exp_month] => 1
                                                    [exp_year] => 2018
                                                    [status] => active
                                                    [created_at] => 2016-11-29T21:14:07Z
                                                    [updated_at] => 2016-11-29T21:14:07Z
                                                    [billing_address] => Array
                                                        (
                                                            [street] => Malibu Point
                                                            [number] => 10880
                                                            [zip_code] => 90265
                                                            [neighborhood] => Central Malibu
                                                            [city] => Malibu
                                                            [state] => CA
                                                            [country] => US
                                                        )

                                                )

                                            [created_at] => 2017-03-24T21:21:58Z
                                            [updated_at] => 2017-03-24T21:21:58Z
                                        )

                                )

                        )

                )

        )

)

2 answers

1

To access the id variable you can do as follows:

echo $data[0]['id'];

This array is multidimencional, that is, to access more "internal" information of the array you can go adding ['kay'] with their respective keys.

You should only use -> to access objects, which is not the case.

1

As Phelipe said, you can access the array element using the element keys:

$value = $data[0]['id'];

But if you want to treat it as an object, you can also do a cast, it would look like this:

$value = ((object)$data[0])->id;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.