【问题标题】:Looping through an array embedded in stdClass遍历嵌入在 stdClass 中的数组
【发布时间】:2012-12-11 14:10:54
【问题描述】:

使用我们提供商的 API,我从 API 调用中获取以下数据:

数据(表示 orderid (1604),然后是订单中的各个项目):

object(stdClass)#395 (1) {  
    ["GetOrderDetailsInfoResult"]=> object(stdClass)#396 (4) {  
        ["RequestID"]=> int(113724)  
        ["Success"]=> int(1)  
        ["Message"]=> string(0) ""  
        ["OrderDetailsResponse"]=> object(stdClass)#397 (1) {  
            ["OrderDetailsResponse"]=> array(2) {  
                [0]=> object(stdClass)#53 (12) {  
                    ["OrderID"]=> int(1604)  
                    ["PartyID"]=> int(0)  
                    ["OrderDetailID"]=> int(805)  
                    ["ProductID"]=> string(3) "B25"  
                    ["Description"]=> string(16) "50 Credits"  
                    ["Quantity"]=> int(1)  
                    ["Price"]=> string(7) "25.0000"  
                    ["Volume"]=> string(7) "25.0000"  
                    ["Tax"]=> string(6) "0.0000"  
                    ["TaxableAmount"]=> string(6) "0.0000"  
                    ["GroupOwner"]=> int(0)  
                    ["ParentOrderDetailID"]=> int(0)  
                }  
                [1]=> object(stdClass)#419 (12) {  
                    ["OrderID"]=> int(1604)  
                    ["PartyID"]=> int(0)  
                    ["OrderDetailID"]=> int(807)  
                    ["ProductID"]=> string(4) "B100"  
                    ["Description"]=> string(17) "200 Credits"  
                    ["Quantity"]=> int(1)  
                    ["Price"]=> string(8) "100.0000"  
                    ["Volume"]=> string(8) "100.0000"  
                    ["Tax"]=> string(6) "0.0000"  
                    ["TaxableAmount"]=> string(6) "0.0000"  
                    ["GroupOwner"]=> int(0)  
                    ["ParentOrderDetailID"]=> int(0)  
                }  
            }  
        }  
    }  
} 

我可以获取到 RequestID,但我无法获取订单的值...如何使用 php 直接获取单个项目?

谢谢。

【问题讨论】:

  • 显示你的代码...为什么你不能得到??
  • 你能像 var_dump() 那样重新输入带有可读缩进的对象吗?

标签: php arrays api loops stdclass


【解决方案1】:

项目可以像这样迭代:

foreach($data->GetOrderDetailsInfoResult->OrderDetailsResponse->OrderDetailsResponse as $item)
{
    echo $item->ProductID;
    echo $item->Description;
    echo $item->Quantity;
    echo $item->Price;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 2014-06-18
    • 2015-10-24
    • 2013-04-23
    • 2014-01-31
    • 1970-01-01
    相关资源
    最近更新 更多