【问题标题】:Get Value of php stdclass object value获取 php stdclass 对象值的值
【发布时间】:2016-01-26 08:44:25
【问题描述】:

刚刚尝试了几个 $result->key ($customer->transaction->status) 但没有成功。我需要 php 中这个对象的“状态”值。尝试了数组移位、->object ->["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"
         }
     }
 }

【问题讨论】:

  • var_dump($customer->transaction); 的结果是什么?
  • thx 4 帮助,问题解决了

标签: php object stdclass


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    相关资源
    最近更新 更多