如何将对象子值分配给变量?

时间:2019-03-14 18:11:29

标签: php oop

我正在使用Stripe创建新的客户和订阅。返回客户和订阅对象时,它们类似于:

Stripe\Customer Object
(
[id] => cus_EhMbQTp34KxvmO
[object] => customer
[account_balance] => 0
[created] => 1552586480
[currency] => 
[default_source] => card_1EDxrkKBfYBcUnN2WtT65hA3
[delinquent] => 
[description] => 
[discount] => 
[email] => alanpollenz@hotmail.com
[invoice_prefix] => FFD0DC3
[invoice_settings] => Stripe\StripeObject Object
    (
        [custom_fields] => 
        [footer] => 
    )

[livemode] => 
[metadata] => Stripe\StripeObject Object
    (
    )

[shipping] => 
[sources] => Stripe\Collection Object
    (
        [object] => list
        [data] => Array
            (
                [0] => Stripe\Card Object
                    (
                        [id] => card_1EDxrkKBfYBcUnN2WtT65hA3
                        [object] => card
                        [address_city] => 
                        [address_country] => 
                        [address_line1] => 
                        [address_line1_check] => 
                        [address_line2] => 
                        [address_state] => 
                        [address_zip] => 
                        [address_zip_check] => 
                        [brand] => Visa
                        [country] => US
                        [customer] => cus_EhMbQTp34KxvmO
                        [cvc_check] => pass
                        [dynamic_last4] => 
                        [exp_month] => 11
                        [exp_year] => 2021
                        [fingerprint] => CNlYQ7G0V42HfZwK
                        [funding] => credit
                        [last4] => 4242
                        [metadata] => Stripe\StripeObject Object
                            (
                            )

                        [name] => alanpollenz@hotmail.com
                        [tokenization_method] => 
                    )

            )

        [has_more] => 
        [total_count] => 1
        [url] => /v1/customers/cus_EhMbQTp34KxvmO/sources
    )

[subscriptions] => Stripe\Collection Object
    (
        [object] => list
        [data] => Array
            (
            )

        [has_more] => 
        [total_count] => 0
        [url] => /v1/customers/cus_EhMbQTp34KxvmO/subscriptions
    )

[tax_info] => 
[tax_info_verification] => 
)


Stripe\Subscription Object
(
[id] => sub_EhMbwAwXReRUo9
[object] => subscription
[application_fee_percent] => 
[billing] => charge_automatically
[billing_cycle_anchor] => 1553882480
[billing_thresholds] => 
[cancel_at] => 
[cancel_at_period_end] => 
[canceled_at] => 
[created] => 1552586480
[current_period_end] => 1553882480
[current_period_start] => 1552586480
[customer] => cus_EhMbQTp34KxvmO
[days_until_due] => 
[default_source] => 
[discount] => 
[ended_at] => 
[items] => Stripe\Collection Object
    (
        [object] => list
        [data] => Array
            (
                [0] => Stripe\SubscriptionItem Object
                    (
                        [id] => si_EhMbNyhkOmti8x
                        [object] => subscription_item
                        [billing_thresholds] => 
                        [created] => 1552586481
                        [metadata] => Stripe\StripeObject Object
                            (
                            )

                        [plan] => Stripe\Plan Object
                            (
                                [id] => 00030
                                [object] => plan
                                [active] => 1
                                [aggregate_usage] => 
                                [amount] => 999
                                [billing_scheme] => per_unit
                                [created] => 1552107233
                                [currency] => usd
                                [interval] => month
                                [interval_count] => 1
                                [livemode] => 
                                [metadata] => Stripe\StripeObject Object
                                    (
                                    )

                                [nickname] => Monthly
                                [product] => prod_EfHkC7lwkSS1l3
                                [tiers] => 
                                [tiers_mode] => 
                                [transform_usage] => 
                                [trial_period_days] => 15
                                [usage_type] => licensed
                            )

                        [quantity] => 1
                        [subscription] => sub_EhMbwAwXReRUo9
                    )

            )

        [has_more] => 
        [total_count] => 1
        [url] => /v1/subscription_items?subscription=sub_EhMbwAwXReRUo9
    )

[latest_invoice] => in_1EDxroKBfYBcUnN2AkQjePZX
[livemode] => 
[metadata] => Stripe\StripeObject Object
    (
    )

[plan] => Stripe\Plan Object
    (
        [id] => 00030
        [object] => plan
        [active] => 1
        [aggregate_usage] => 
        [amount] => 999
        [billing_scheme] => per_unit
        [created] => 1552107233
        [currency] => usd
        [interval] => month
        [interval_count] => 1
        [livemode] => 
        [metadata] => Stripe\StripeObject Object
            (
            )

        [nickname] => Monthly
        [product] => prod_EfHkC7lwkSS1l3
        [tiers] => 
        [tiers_mode] => 
        [transform_usage] => 
        [trial_period_days] => 15
        [usage_type] => licensed
    )

[quantity] => 1
[schedule] => 
[start] => 1552586480
[status] => trialing
[tax_percent] => 
[trial_end] => 1553882480
[trial_start] => 1552586480
)

我是oop的菜鸟,但是我设法设置了一些变量以插入到mySQL表中,例如

$customerID = $customer->id;

但是我将如何获得订阅昵称值(从底部起约15行)。我尝试了各种迭代,例如:

$nickname = $subscription->plan->nickname;

但似乎没有任何作用。

有什么想法吗?

谢谢。

艾伦

根据Patrick的评论进行编辑:

var_dump($subscription->plan->nickname);

输出:

string(7) "Monthly

,然后执行以下操作:

$output .= "Subscription Plan ID = $subscription->id<br />";
$output .= "Subscription Plan Nickname = $subscription->plan->nickname<br />";
echo $output;

输出:

Subscription Plan ID = sub_EhN9GgJfiFYmsV Subscription Plan

Nickname = Stripe\Plan JSON: { "id": "00030", "object": "plan", "active": true, "aggregate_usage": null, "amount": 999, "billing_scheme": "per_unit", "created": 1552107233, "currency": "usd", "interval": "month", "interval_count": 1, "livemode": false, "metadata": [], "nickname": "Monthly", "product": "prod_EfHkC7lwkSS1l3", "tiers": null, "tiers_mode": null, "transform_usage": null, "trial_period_days": 15, "usage_type": "licensed" }->nickname

2 个答案:

答案 0 :(得分:0)

由于帕特里克(Patrick),我得以提出以下似乎可行的方法:

$nickname = print_r($subscription->plan->nickname, true);
$output .= "Subscription Plan Nickname = $nickname<br />";
echo $output;

结果:

Subscription Plan Nickname = Monthly

答案 1 :(得分:-1)

由于尝试访问嵌套数组,因此需要先将所需的值分配给变量,然后将其注入字符串中,或​​者需要将对象访问位用大括号括起来。

因此您可以执行以下操作:

$planNickname = $subscription->plan->nickname;
$output .= "Subscription Plan ID = $subscription->id<br />";
$output .= "Subscription Plan Nickname = $planNickname<br />";
echo $output;

或者您可以这样做:

$output .= "Subscription Plan ID = $subscription->id<br />";
$output .= "Subscription Plan Nickname = {$subscription->plan->nickname}<br />";
echo $output;