dongtang1944 2019-03-14 18:11
浏览 61
已采纳

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

I am creating a new customer and subscription using Stripe. When the customer and subscription objets are returned, they look like:

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
)

I am a noob with oop, but I have managed to set some variables for insertion into a mySQL table, such as

$customerID = $customer->id;

But how would I get the subscription nickname value (about 15 lines up from the bottom). I've tried various iterations, like:

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

but nothing seems to work.

Any ideas?

Thanks.

Alan

Edit based on Patrick's comments:

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

outputs:

string(7) "Monthly

and then following :

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

outputs:

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条回答 默认 最新

  • douxin9135 2019-03-14 18:54
    关注

    Since you are attempting to access nested arrays, you need to either assign the desired value to a variable first, and then inject it into the string, or you need to wrap the object access bit in braces.

    So you can either do:

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

    Or you can do:

    $output .= "Subscription Plan ID = $subscription->id<br />";
    $output .= "Subscription Plan Nickname = {$subscription->plan->nickname}<br />";
    echo $output;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导