PHP访问数组中的项

时间:2014-01-03 16:53:55

标签: php arrays

我有以下数组:

Cart Object
(
    [event_list] => Array
        (
            [15] => stdClass Object
                (
                    [event_id] => 15
                    [event_name] => North Pole Express 2014
                    [event_date] => 2014-12-06
                    [event_time] => 10:40:00
                    [event_ort_id] => 1
                    [ort_name] => Tanfield Railway
                    [ort_city] => Newcastle upon Tyne
                    [event_order_limit] => 0
                    [event_use_alt] => 
                )

            [14] => stdClass Object
                (
                    [event_id] => 14
                    [event_name] => North Pole Express 2014
                    [event_date] => 2014-11-30
                    [event_time] => 10:40:00
                    [event_ort_id] => 1
                    [ort_name] => Tanfield Railway
                    [ort_city] => Newcastle upon Tyne
                    [event_order_limit] => 0
                    [event_use_alt] => 
                )

            [13] => stdClass Object
                (
                    [event_id] => 13
                    [event_name] => North Pole Express 2014
                    [event_date] => 2014-11-29
                    [event_time] => 10:40:00
                    [event_ort_id] => 1
                    [ort_name] => Tanfield Railway
                    [ort_city] => Newcastle upon Tyne
                    [event_order_limit] => 0
                    [event_use_alt] => 
                )

        )

    [cat_list] => Array
        (
            [138] => stdClass Object
                (
                    [cat_id] => 138
                    [category_event_id] => 15
                    [cat_name] => Child - 4:00 pm
                    [cat_price] => 12.00
                    [cat_numbering] => none
                )

            [120] => stdClass Object
                (
                    [cat_id] => 120
                    [category_event_id] => 14
                    [cat_name] => Child - 4:00 pm
                    [cat_price] => 12.00
                    [cat_numbering] => none
                )

            [102] => stdClass Object
                (
                    [cat_id] => 102
                    [category_event_id] => 13
                    [cat_name] => Child - 4:00 pm
                    [cat_price] => 12.00
                    [cat_numbering] => none
                )

        )

    [disc_list] => Array
        (
        )

    [items] => Array
        (
            [4] => PlaceItem Object
                (
                    [id] => 4
                    [cart] => Cart Object
 *RECURSION*
                    [event_id] => 14
                    [category_id] => 120
                    [seats] => Array
                        (
                            [26151] => stdClass Object
                                (
                                    [seat_id] => 26151
                                    [seat_row_nr] => 0
                                    [seat_nr] => 0
                                    [seat_ts] => 1388769219
                                    [discount_id] => 0
                                )

                        )

                    [ts] => 
                    [created] => 2014-01-03T16:43:39+00:00
                    [expired] => 
                )

        )

    [ts] => 1388769219
)

我需要能够访问'Items'键。

然而,到目前为止,我所尝试的一切都失败了。我怀疑我错过了一些非常明显的东西。

访问这些项目的最佳方式是什么?

2 个答案:

答案 0 :(得分:3)

Cart不是数组,它是一个Object。

$cart = new Cart();
$cart->items; // <-- this is an array (of PlaceItems)

答案 1 :(得分:1)

由于$cart是一个对象,您需要使用对象表示法来访问它,因此数组位于下面:

forach ($cart->items as $key=>$value) {
  //...
}

从评论中更新:

$items=$_SESSION['_SMART_cart']->items;
foreach ($items as $key=>$value) {
   //...
}