0

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] => [email protected]
                            [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] => [email protected]
                            [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?

2

1 Answer 1

2

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");
 }
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to use foreach
I also used for each for the same data set

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.