循环遍历stdClass中嵌入的数组

时间:2012-12-25 19:35:15

标签: php arrays api loops stdclass

使用我们提供商提供的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直接获取各个项目?

感谢。

1 个答案:

答案 0 :(得分:0)

这些项目可以像这样迭代:

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