开发者

Access array elements from API

开发者 https://www.devze.com 2023-03-04 04:03 出处:网络
For the life of me I can not figure out how to access the values of this array.Every example the stdClass Object has some type of value.If I try for example $obj->0->0->city; I get an error.

For the life of me I can not figure out how to access the values of this array. Every example the stdClass Object has some type of value. If I try for example $obj->0->0->city; I get an error.

Can someone show me a example how to access [city] => toronto or even [date_created] => 2011-05-03 14:33:58?

I also tried this with no luck.

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

Thanks

This is what the api gives me.

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


There is a special evil syntax to bypass numeric object attributes:

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

Is the correct syntax, and equivalent to the path you already determined.

Your second example is an array however, so it's probably:

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

The alternative is always to just foreach over a specific level - that works for objects and arrays likewise.

0

精彩评论

暂无评论...
验证码 换一张
取 消