2013-05-18 128 views
3

我試圖訪問從API返回的PHP對象的值。我正試圖獲得'全面'的屬性。從php對象訪問@attribute

stdClass Object 
(
    [@attributes] => stdClass Object 
     (
      [status] => ok 
     ) 

    [invoices] => stdClass Object 
     (
      [@attributes] => stdClass Object 
       (
        [page] => 1 
        [per_page] => 25 
        [pages] => 1 
        [total] => 5 
       ) 

我返回的對象存儲在名爲$ list的變量中。

$list->invoices->attributes->total 

我想echo/print_r,但什麼都沒有?

任何幫助表示讚賞!

回答

6

@是屬性名稱的一部分,您不能忽略它。

echo $list->invoices->{'@attributes'}->total; 
+0

你可以只定義@attribute使用變量名的變量和訪問。示例'$ abc ='@attributes'; echo $ list-> invoices - > $ abc-> total;' – vijayrana

3
$total = $list->invoices->attributes()->total; 
0

事實證明,你不需要即使指定@屬性來讀取這些數據。實現它的關鍵技巧是將結果作爲字符串進行投射。

所以,我知道這似乎很奇怪,但是這會在這種情況下工作:

echo (string)$list->invoices['total'];