获取php stdclass对象值的值

时间:2016-01-26 08:44:26

标签: php object stdclass

刚试了好几个$ result->键($ customer-> transaction->状态),但没有成功。我需要在php中使用此对象的“status”值。尝试了一些阵列移位的组合, - >对象 - > [“状态”]等等。

 object(stdClass)#2 (3) {
     ["customer"]=>
     object(stdClass)#3 (1) {
         ["link"]=>
         object(stdClass)#4 (3) {
             ["url"]=>
             string(78) "https://demo1.com"
             ["rel"]=>
             string(8) "customer"
             ["method"]=>
             string(3) "GET"
         }
     }
     ["transaction"]=>
     object(stdClass)#5 (9) {
         ["merchantRefId"]=>
         string(19) "46532156465456"
         ["amount"]=>
         int(200)
         ["currency"]=>
         string(3) "EUR"
         ["id"]=>
         string(15) "646544564564"
         ["transactionType"]=>
         string(27) "Transfer"
         ["createDate"]=>
         string(19) "2016-01-26 08:33:09"
         ["updateDate"]=>
         string(19) "2016-01-26 08:33:09"
         ["status"]=>
         string(8) "accepted"
         ["fees"]=>
         array(1) {
             [0]=>
             object(stdClass)#6 (3) {
                 ["feeType"]=>
                 string(11) "service_fee"
                 ["feeAmount"]=>
                 int(119)
                 ["feeCurrency"]=>
                 string(3) "EUR"
             }
         }
     }
     ["links"]=>
     array(1) {
         [0]=>
         object(stdClass)#7 (3) {
             ["url"]=>
             string(78) "https://demo.com"
             ["rel"]=>
             string(4) "self"
             ["method"]=>
             string(3) "GET"
         }
     }
 }

1 个答案:

答案 0 :(得分:1)

由于您没有提供创建对象的代码,因此我创建了JSON字符串并将其转换为对象,从而为您提供类似的var_dump结果。

<?php
    $jsonStr = '{
    "customer": {
        "link": {
            "url": "https://demo1.com",
            "rel": "customer",
            "method": "GET"
        }
    },
    "transaction": {
        "merchantRefId": "46532156465456",
        "amount": 200,
        "currency": "EUR",
        "id": "646544564564",
        "transactionType": "Transfer",
        "createDate": "2016-01-26 08:33:09",
        "updateDate": "2016-01-26 08:33:09",
        "status": "accepted",
        "fees": [
            {
              "feeType": "service_fee",
              "feeAmount": 119,
              "feeCurrency": "EUR"
            }
        ]
    },
    "links": [
        {
            "url": "https://demo.com",
            "rel": "self",
            "method": "GET"
        }
    ]
}';

$stdObj = json_decode($jsonStr);        
var_dump($stdObj);        
var_dump($stdObj->transaction->status);

我能够,你也应该这样做,只需简单地获得status

$customer->transaction->status