棘手的PHP数组

时间:2017-11-21 19:12:11

标签: php arrays

我使用stripe来返回一个Customer对象。我想提取银行帐户ID并将其保存在数据库中供以后使用。正确访问数组的正确语法是什么?

以下是我的尝试:

$bank_account=$customer->lastResponse['json']['sources']['data'][0]['id'];

有人告诉我试试:

$bank_account=$customer->bank_accounts->data[0]['id'];

但是这也是空的......

我不确定如何使用该对象或正确的方法是什么。 我需要bank_account_id 这是var_dump($customer)在进行一些格式化和删除不相关的信息后的样子。我想我可以使用JSON解码或访问名为' json'我不知道这些解决方案的正确语法。请帮忙(:

object(Stripe\Customer)#3572 (6) 
{ 
    ["_opts":protected]=> object(Stripe\Util\RequestOptions)#3576 (2) { ["headers"]=> array(1) { ["Stripe-Account"]=> string(21) "acct_1BNn74AprSj6yALS" } ["apiKey"]=> string(32) "secretkey" } 
    ["_values":protected]=> array(15) {//Removed For brevity} 
    ["_unsavedValues":protected]=> object(Stripe\Util\Set)#3590 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_transientValues":protected]=> object(Stripe\Util\Set)#3602 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_retrieveOptions":protected]=> array(0) { } 
    ["_lastResponse":protected]=> 
        object(Stripe\ApiResponse)#3714 (4) 
        { 
            ["headers"]=> array(15) { //Removed for brevity} 
            // Json
            ["body"]=> string(1148) "{
                "id": "cus_BoJOjFghfV7mmq", "object": "customer", "account_balance": 0, "created": 1511290036, "currency": null, "default_source": "bank_account_id", "delinquent": false, "description": "Name", "discount": null, "email": null, "livemode": true, "metadata": {}, "shipping": null, "sources": { "object": "list", "data": [ { "id": "bank_account_id", "object": "bank_account", "account_holder_name": "Daniel Taylor", "account_holder_type": "individual", "bank_name": "Bank Name", "country": "US", "currency": "usd", "customer": "cus_id", "fingerprint": "info", "last4": "last four of account", "metadata": {}, "routing_number": "routing number", "status": "new" } ], "has_more": false, "total_count": 1, "url": "/v1/customers/cus_BoJOjFghfV7mmq/sources" }, "subscriptions": { "object": "list", "data": [], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_BoJOjFghfV7mmq/subscriptions" } 
            } " 
            ["json"]=> array(15) 
            { 
                ["id"]=> string(18) "cus_id" 
                ["object"]=> string(8) "customer" 
                ["account_balance"]=> int(0) 
                ["created"]=> int(1511290036) 
                ["currency"]=> NULL 
                ["default_source"]=> string(27) "bank_account_id" 
                ["delinquent"]=> bool(false) 
                ["description"]=> string(13) "Name" 
                ["discount"]=> NULL 
                ["email"]=> NULL ["livemode"]=> bool(true) 
                ["metadata"]=> array(0) { } 
                ["shipping"]=> NULL 
                ["sources"]=> array(5) 
                { 
                    ["object"]=> string(4) "list" 
                    ["data"]=> array(1) 
                    { 
                        [0]=> array(13) 
                        {
                            ["id"]=> string(27) "bank_account_id" ["object"]=> string(12) "bank_account" 
                            ["account_holder_name"]=> string(13) "name" 
                            ["account_holder_type"]=> string(10) "individual" 
                            ["bank_name"]=> string(26) "Bank Name" 
                            ["country"]=> string(2) "US" ["currency"]=> string(3) "usd" 
                            ["customer"]=> string(18) "cus_ID" 
                            ["fingerprint"]=> string(16) "fingerprint" 
                            ["last4"]=> string(4) "lastfour" ["metadata"]=> array(0) { } 
                            ["routing_number"]=> string(9) "routenumber" 
                            ["status"]=> string(3) "new" 
                        } 
                    } 
                    ["has_more"]=> bool(false) 
                    ["total_count"]=> int(1) 
                    ["url"]=> string(40) "/v1/customers/cus_BoJOjFghfV7mmq/sources" 
                } 
                ["subscriptions"]=> array(5) { ["object"]=> string(4) "list" ["data"]=> array(0) { } ["has_more"]=> bool(false) ["total_count"]=> int(0) ["url"]=> string(46) "/v1/customers/cus_id/subscriptions" } 
            } 
            ["code"]=> int(200) 
        } 
}

2 个答案:

答案 0 :(得分:0)

我认为应该是:

$bank_account = $customer->sources->data[0]->id

如果客户有多个资金来源,您可能需要遍历data数组以找到您想要的资金来源。对于您想要的来源,$customer->sources->data[$i]->object将为"bank_account"

答案 1 :(得分:0)

很确定那里有吸气剂。您可以使用对象所期望的属性。

示例:

$customer_id = $customer->id;
$bank_account_id = $customer->sources->data[0]->id;