Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为条带设置Json对象中的PHP变量_Php_Json_Stripe Payments - Fatal编程技术网

为条带设置Json对象中的PHP变量

为条带设置Json对象中的PHP变量,php,json,stripe-payments,Php,Json,Stripe Payments,这里的PHP学生第一次使用json。Stripe使用Json发送数据,我尝试将特定对象设置为PHP变量。例如,在这个Json结果中,如何将变量设置为“订阅”->“金额”中给出的“5000”: object: "customer", created: 1410050969, id: cus_4jMblEOo2y84j6, livemode: false, description:

这里的PHP学生第一次使用json。Stripe使用Json发送数据,我尝试将特定对象设置为PHP变量。例如,在这个Json结果中,如何将变量设置为“订阅”->“金额”中给出的“5000”:

            object: "customer",
            created: 1410050969,
            id: cus_4jMblEOo2y84j6,
            livemode: false,
            description: "payinguser@example.com",
            email: "sample24@gmail.com",
            delinquent: false,
            metadata:
            {}
            subscriptions::
            {
                object: "list"
                total_count: 1
                has_more: false
                url: "/v1/customers/cus_4jMblEOo2y84j6/subscriptions"
                data:
                [
                    {
                        id: sub_4jMbSoTXxUBHd0
                        plan:
                        {
                            interval: "year",
                            name: "Website Hosting",
                            created: 1409981699,
                            amount: 5000,
                            currency: "usd",
                            id: webhosting,
                            object: "plan",
                            livemode: false,
                            interval_count: 1,
                            trial_period_days: null,
                            metadata:
                            {},
                            statement_description: null
                        },
                        count:1
            }
我最好的失败尝试是:

    $amount = customer->subscriptions->data->plan->amount;
    $amount = $event_json->customer->subscriptions->data->plan->amount;
    $amount = subscriptions->data->plan->amount;
    $amount = $event_json->subscriptions->data->plan->amount;

非常感谢您的任何想法或通用json/php参考资料对本例有所帮助

金额
位于订阅的
数据
数组中,因此它看起来像:

$customer->subscriptions->data[0]->plan->amount=5000

所以这里的关键是
数据
是订阅对象的数组,因此您需要确定要设置值的对象中的哪一个-如果只有一个,则可以使用键
0
,如果有多个,则需要对它们进行筛选以获得所需的值(除非您知道特定的键,在这种情况下,您可以使用该键而不是
0
)。例如:

$subid = 'sub_4jMbSoTXxUBHd0';
foreach ($customer->subscriptions->data as $k => $sub) {
   if ($sub->id === $subid) {
     $customer->subscriptions->data[$k]->plan->amount = 5000;
     break;
   }
}
另一个更容易处理的方法是将所有JSON对象作为数组保留在PHP端,这样就不需要根据类型进行不同的访问

// by passing true as the second arg js objects get converted to associative arrays
$customer = json_decode($jsonString, true);
$subid = 'sub_4jMbSoTXxUBHd0';

// now we can use array notation for objects instead of ->
foreach ($customer['subscriptions']['data'] as $k => $sub) {
   if ($sub->id === $subid) {
     $customer['subscriptions']['data'][$k]['plan']['amount'] = 5000;
     break;
   }
}