0

PHP student here using json for the first time. Stripe is sending data using Json and I am trying to set specific objects to my PHP variables. For example, how would I set a variable to the "5000" given from "subscriptions" -> "amount" in this Json result:

            object: "customer",
            created: 1410050969,
            id: cus_4jMblEOo2y84j6,
            livemode: false,
            description: "[email protected]",
            email: "[email protected]",
            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
            }

My best failed attempts:

    $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;

Any ideas or general json/php references that would help in this case are greatly appreciated!

1 Answer 1

1

amount is in the data array of a subscription so it would look like:

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

So key thing here is that data is an array of subscription objects so you need to determine which one of those you want to set the value - if there is only one then you can use the key 0 if there are multiple then you need to filter through them to get the one you want (unless you otherwise know the specific key in which case you can use that instead of 0). For example:

$subid = 'sub_4jMbSoTXxUBHd0';
foreach ($customer->subscriptions->data as $k => $sub) {
   if ($sub->id === $subid) {
     $customer->subscriptions->data[$k]->plan->amount = 5000;
     break;
   }
}

Another thing that makes dealing with this easier is to to keep all JSON objects as arrays on the PHP side so that you dont need to access things differently based on type. to do this you can use the second argument to json_decode:

// 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;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.