1

I seem to be having some trouble with JSON arrays. I was wondering if someone could please assist me.

I have the following JSON Array, and I want to get the figure in invoice_number

$json = '
{
    "id": "PAY-7MP775806F4135612LLCHE4I",
    "intent": "sale",
    "state": "approved",
    "cart": "9KA22662P3559221J",
    "payer": 
    {
        "payment_method": "paypal",
        "status": "VERIFIED",
        "payer_info": 
        {
            "email": "[email protected]",
            "first_name": "test",
            "last_name": "buyer",
            "payer_id": "ZR6SRXGS252RG",
            "shipping_address": 
            {
                "recipient_name": "test buyer",
                "line1": "1 Cheeseman Ave Brighton East",
                "city": "Melbourne",
                "state": "Victoria",
                "postal_code": "3001",
                "country_code": "AU"
            },
            "phone": "0364424947",
            "country_code": "AU"
        }
    },
    "transactions": 
    [
        {
            "amount": 
            {
                "total": "3.00",
                "currency": "USD",
                "details": 
                {
                    "subtotal": "3.00"
                }
            },
            "payee": 
            {
                "merchant_id": "DSTEYCMCDUL3Y"
            },
            "description": "Payment description",
            "invoice_number": "abc1231522823790",
            "item_list": 
            {
                "items": 
                [
                    {
                        "name": "Subscribe",
                        "sku": "sub1",
                        "price": "3.00",
                        "currency": "USD",
                        "tax": "0.00",
                        "quantity": 1
                    }
                ],
                "shipping_address": 
                {
                    "recipient_name": "test buyer",
                    "line1": "1 Cheeseman Ave Brighton East",
                    "city": "Melbourne",
                    "state": "Victoria",
                    "postal_code": "3001",
                    "country_code": "AU"
                }
            },
            "related_resources": 
            [
                {
                    "sale": 
                    {
                        "id": "41B66647LJ233225Y",
                        "state": "completed",
                        "amount": 
                        {
                            "total": "3.00",
                            "currency": "USD",
                            "details": 
                            {
                                "subtotal": "3.00"
                            }
                        },
                        "payment_mode": "INSTANT_TRANSFER",
                        "protection_eligibility": "ELIGIBLE",
                        "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
                        "transaction_fee": 
                        {
                            "value": "0.37",
                            "currency": "USD"
                        },
                        "parent_payment": "PAY-7MP775806F4135612LLCHE4I",
                        "create_time": "2018-04-04T06:36:59Z",
                        "update_time": "2018-04-04T06:36:59Z",
                        "links": 
                        [
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y",
                                "rel": "self",
                                "method": "GET"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y/refund",
                                "rel": "refund",
                                "method": "POST"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
                                "rel": "parent_payment",
                                "method": "GET"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "create_time": "2018-04-04T06:36:33Z",
    "links": 
    [
        {
            "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
            "rel": "self",
            "method": "GET"
        }
    ]
}';

The code I was trying to use is but it comes up with error Undefined property: stdClass::$invoice_number

$decoded = json_decode($json);
echo $Info = $decoded->transactions[0]->payee->invoice_number;

Could someone please tell me why this does not work?

Thanks

Robert

3
  • what error? please add to question. Commented Apr 5, 2018 at 1:53
  • Undefined property: stdClass::$invoice_number Commented Apr 5, 2018 at 1:55
  • 2
    They’re right, there’s no invoice_number in payee, it’s in transactions Commented Apr 5, 2018 at 2:00

1 Answer 1

1
echo $Info = $decoded->transactions[0]->invoice_number;

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome. You are a legend. Thanks
@BooberBunz by the way Web Store of google chrome has this Json Editor for free offline usage :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.