Access values within an array

0

I am having a question about accessing a variable within an array, I am a beginner in the PHP world (server side only without HTML) and I am having to handle a response from the server.

I'm not getting basic access to the variable

If I try

echo $data[0]->id

returns

Trying to get property of non-object

The second answer_r array of print_r

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
                                        )

                                )

                        )

                )

        )

)
    
asked by anonymous 22.01.2018 / 21:39

2 answers

1

To access the variable id you can do the following:

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

This array is multidimensional, that is, to access more "internal" information from the array you can add ['kay'] to its array.

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

    
22.01.2018 / 21:42
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 cast it, it looks like this:

$value = ((object)$data[0])->id;
    
22.01.2018 / 23:34