Skip to content
Advertisement

How to get values from array object in PHP

I have the following array and when I do print("<pre>".print_r($term_arr,true)."</pre>");, I get:

Array
(
    [0] => Array
        (
            [status] => 1
            [message] => Charge attempted
            [data] => Array
                (
                    [amount] => 1000000
                    [currency] => NGN
                    [transaction_date] => 2021-05-04T03:18:03.000Z
                    [status] => success
                    [reference] => tbhlnqs9elbcoth
                    [domain] => test
                    [metadata] => 
                    [gateway_response] => Approved
                    [message] => 
                    [channel] => card
                    [ip_address] => 
                    [log] => 
                    [fees] => 25000
                    [authorization] => Array
                        (
                            [authorization_code] => AUTH_z260f8cskt
                            [bin] => 408408
                            [last4] => 4081
                            [exp_month] => 12
                            [exp_year] => 2030
                            [channel] => card
                            [card_type] => visa 
                            [bank] => TEST BANK
                            [country_code] => NG
                            [brand] => visa
                            [reusable] => 1
                            [signature] => SIG_TyzxLul2N9M3RSX5MJIY
                            [account_name] => 
                        )

                    [customer] => Array
                        (
                            [id] => 43591782
                            [first_name] => 
                            [last_name] => 
                            [email] => ele48@gmail.com
                            [customer_code] => CUS_rs2vvoeo7pe6f1b
                            [phone] => 
                            [metadata] => 
                            [risk_action] => default
                            [international_format_phone] => 
                        )

                    [plan] => 
                    [id] => 1112036335
                )

        )

    [1] => Array
        (
            [status] => 1
            [message] => Charge attempted
            [data] => Array
                (
                    [amount] => 100000
                    [currency] => NGN
                    [transaction_date] => 2021-05-04T03:18:03.000Z
                    [status] => success
                    [reference] => fxi85hsbg2u8jwi
                    [domain] => test
                    [metadata] => 
                    [gateway_response] => Approved
                    [message] => 
                    [channel] => card
                    [ip_address] => 
                    [log] => 
                    [fees] => 1500
                    [authorization] => Array
                        (
                            [authorization_code] => AUTH_bn008ru8rq
                            [bin] => 408408
                            [last4] => 4081
                            [exp_month] => 12
                            [exp_year] => 2030
                            [channel] => card
                            [card_type] => visa 
                            [bank] => TEST BANK
                            [country_code] => NG
                            [brand] => visa
                            [reusable] => 1
                            [signature] => SIG_TyzxLul2N9M3RSX5MJIY
                            [account_name] => 
                        )

                    [customer] => Array
                        (
                            [id] => 43746063
                            [first_name] => 
                            [last_name] => 
                            [email] => gabwebby@gmail.com
                            [customer_code] => CUS_d4r1mko8twyc6vg
                            [phone] => 
                            [metadata] => 
                            [risk_action] => default
                            [international_format_phone] => 
                        )

                    [plan] => 
                    [id] => 1112036343
                )

        )

I am trying to get the value of customer, so I did the following:

foreach($term_arr as $value){
       echo 'Title: ' . $value->customer->customer_code .'<br/>';
  }

But I have this error:

PHP Notice: Trying to get property ‘customer’ of non-object in PHP Notice: Trying to get property ‘customer_code’ of non-object in

How can I get the values of array object?

Advertisement

Answer

Firstly your inner array is associatve array so if we want values to get we need to traverse using key.

Solution you tried to get customer code where hierarchy is not proper

When you iterate over you parent index array hierarchy is data->customer->customer code

so your customer resides under data array thats why you are getting notice that customer is non object of value.

So solution or you can iterate like will give you customer code,

foreach($term_arr as $value){
     print_r($value["data"]["customer"]["customer_code"]."n");
 }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement