PHP从多层JSON数组中获取值

时间:2018-04-05 01:51:12

标签: php arrays json

我似乎在使用JSON数组时遇到了一些麻烦。我想知道是否有人可以帮助我。

我有以下JSON数组,我想获取invoice_number中的数字

$json = '
{
    "id": "PAY-7MP775806F4135612LLCHE4I",
    "intent": "sale",
    "state": "approved",
    "cart": "9KA22662P3559221J",
    "payer": 
    {
        "payment_method": "paypal",
        "status": "VERIFIED",
        "payer_info": 
        {
            "email": "accounts-buyer@traxprint.com",
            "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"
        }
    ]
}';

我试图使用的代码是错误的未定义属性:stdClass :: $ invoice_number

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

有人可以告诉我为什么这不起作用吗?

由于

罗伯特

1 个答案:

答案 0 :(得分:1)

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

enter image description here