2011-05-10 88 views
0

對於我的生活,我無法弄清楚如何訪問這個數組的值。每個示例stdClass對象都具有某種類型的值。如果我嘗試例如$obj->0->0->city;,則出現錯誤。訪問API的數組元素

有人可以告訴我一個例子如何訪問[city] => toronto甚至[date_created] => 2011-05-03 14:33:58

我也試過這個沒有運氣。

$object = $buy[1]; 
$title = $object->title[0]; 
echo "$title"; 

感謝

這就是API給我。

stdClass Object 
(
    [id] => 1 
    [name] => toronto 
    [date_modified] => 2011-03-08 13:07:10 
    [tax_rate_provincial] => 
) 
<br/> 
Array 
(
    [0] => stdClass Object 
     (
      [0] => stdClass Object 
       (
        [id] => 28131844 
        [full_date] => 20110506 
        [end_date] => 20110511 
        [city] => toronto 
        [saved] => 1651 
        [discount_percentage] => 52 
        [deal_option] => Array 
         (
          [0] => stdClass Object 
           (
            [id] => 2600 
            [title] => 
            [date_modified] => 0000-00-00 00:00:00 
            [date_created] => 2011-05-03 14:33:58 
            [value] => 3150 
            [price] => 1499 
            [deal_id] => 28131844 
            [is_default] => 0 
           ) 

         ) 

        [options] => 
        [option_quantity] => 
        [option_remaining] => 
        [purchase_limit] => 1 
        [gift_limit] => 0 

回答

2

有一個特別邪惡的語法來繞過數字對象的屬性:

print $obj->{'0'}->{'0'}->city; 

是正確的語法,以及相當於你已經確定的路徑。

你的第二個例子是一個數組然而,因此它可能是:

print $array[0]->{'0'}->city; 

另一種方法是始終只是foreach在一個特定的水平 - 即對對象和數組同樣適用。