使用其他对象访问数组中的对象及其Values.Squareup返回Object api

时间:2017-02-17 22:58:50

标签: php arrays object

如何访问square up api return对象中的值。

通常我会这样做:$ result持有对象数组

echo $result->id;

但这似乎不起作用:

使用print_r()

时返回对象
SquareConnect\Model\ChargeResponse Object
(
    [errors:protected] => 
    [transaction:protected] => SquareConnect\Model\Transaction Object
        (
            [id:protected] => 1111111-15f3-5b37-6940-ceac7a6b54b8 //modified for stackoverflow
            [location_id:protected] => 111111111111111111111111111111 //modified
            [created_at:protected] => 2017-02-17T22:48:15Z
            [tenders:protected] => Array
                (
                    [0] => SquareConnect\Model\Tender Object
                        (
                            [id:protected] => 111111-3e5f-1111111-710c-4a4f11111d7be //modified for stack overflow
                            [location_id:protected] => 111111Nz8yXzf7rZHFYx0mbIKEgAQ //modified for stack overflow
                            [transaction_id:protected] => 111111111-15f3-5b37-6940-11111 //modified for stack overflow
                            [created_at:protected] => 2017-02-17T22:48:15Z
                            [note:protected] => Online Transaction
                            [amount_money:protected] => SquareConnect\Model\Money Object
                                (
                                    [amount:protected] => 402
                                    [currency:protected] => USD
                                )

                            [processing_fee_money:protected] => 
                            [customer_id:protected] => 
                            [type:protected] => CARD
                            [card_details:protected] => SquareConnect\Model\TenderCardDetails Object
                                (
                                    [status:protected] => CAPTURED
                                    [card:protected] => SquareConnect\Model\Card Object
                                        (
                                            [id:protected] => 
                                            [card_brand:protected] => VISA
                                            [last_4:protected] => 5858
                                            [exp_month:protected] => 
                                            [exp_year:protected] => 
                                            [cardholder_name:protected] => 
                                            [billing_address:protected] => 
                                        )

                                    [entry_method:protected] => KEYED
                                )

                            [cash_details:protected] => 
                        )

                )

            [refunds:protected] => 
            [reference_id:protected] => 
            [product:protected] => EXTERNAL_API
            [client_id:protected] => 
            [order:protected] => 
            [shipping_address:protected] => 
        )

)

使用print($ result);

{
    "transaction": {
        "id": "9b2f3df1-cdf3-5399-6c54-07a67af8d8f2",
        "location_id": "CBASEGjNz8yXzf7rZHFYx0mbIKEgAQ",
        "created_at": "2017-02-17T23:37:28Z",
        "tenders": [
            {
                "id": "c424e20e-dac0-5144-518a-114fe24cb2e5",
                "location_id": "CBASEGjNz8yXzf7rZHFYx0mbIKEgAQ",
                "transaction_id": "9b2f3df1-cdf3-5399-6c54-07a67af8d8f2",
                "created_at": "2017-02-17T23:37:28Z",
                "note": "Online Transaction",
                "amount_money": {
                    "amount": 402,
                    "currency": "USD"
                },
                "type": "CARD",
                "card_details": {
                    "status": "CAPTURED",
                    "card": {
                        "card_brand": "VISA",
                        "last_4": "5858"
                    },
                    "entry_method": "KEYED"
                }
            }
        ],
        "product": "EXTERNAL_API"
    }
}

如何访问square up api中的不同数组和对象,以便我可以使用此信息。所以为了简短回答,我如何在Tenders Object中访问id或对象事务id?

1 个答案:

答案 0 :(得分:0)

api没有谈到访问返回的值,所以我做了一些试验和错误。最后发现,这是一个多元文件,所以做了类似的事情:$result['transaction']['tenders'][0]['type'] 允许我访问返回数组的值。