简体   繁体   English

如何从 PHP 中的数组 object 中获取值

[英]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>");我有以下数组,当我执行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 PHP 注意:试图在 PHP 中获取属性“客户”的非对象

How can I get the values of array object?如何获取数组 object 的值?

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.所以您的客户位于数据数组下,这就是您注意到客户不是 object 价值的原因。

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");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM