Skip to content
Advertisement

PHP: How to print sub values from multi-level array (Stripe object)

I get the below object which returns from retrieving a Stripe session ID (the object is called $order).

I am able to print values from the first object level using e.g. print_r($order->id);

How can I print values from the second level, such as amount or currency ?
I tried print_r($order->display_items->amount); and print_r($order->display_items['amount']);

PHP:

StripeCheckoutSession JSON: { 
    "id": "some_id", 
    "object": "checkout.session", 
    "billing_address_collection": null, 
    "cancel_url": "some_url", 
    "client_reference_id": null, 
    "customer": "some_id", 
    "customer_email": null, 
    "display_items": [{
        "amount": 2990, // this is what I need
        "currency": "eur", // this is what I need
        "custom": {
            "description": null, 
            "images": null, 
            "name": "some_product"
        },
        "quantity": 1, 
        "type": "custom"
    }], 
    "livemode": false, 
    "locale": "en", 
    "mode": "payment", 
    "payment_intent": "some_id", 
    "payment_method_types": [ "card" ], 
    "setup_intent": null, 
    "submit_type": null, 
    "subscription": null, 
    "success_url": "some_url"
}

Update: If I use print_r($order->display_items); I get the below. Could I assign this to an array and then print from there (if there is no other solution) ?

Array ( [0] => StripeStripeObject Object ( [amount] => 2990 [currency] => eur [custom] => StripeStripeObject Object ( [description] => [images] => [name] => some_product ) [quantity] => 1 [type] => custom ) )

Many thanks in advance.

Advertisement

Answer

print_r($obj->display_items[0]->amount);

Just be sure your JSON is valid 🙂 https://jsonlint.com/

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement