在Doctrine arraycollection中使用foreach循环

时间:2014-11-07 12:00:45

标签: php doctrine-orm

我刚刚发现了以下库https://github.com/simshaun/recurr,输出正在使用Doctrine arrayCollection

如何使用foreach循环遍历此数组并获取日期值?

Array
 (
    [0] => Recurr\Recurrence Object
        (
        [start:protected] => DateTime Object
            (
                [date] => 2014-08-03 15:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

        [end:protected] => DateTime Object
            (
                [date] => 2014-08-03 17:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

    )

[1] => Recurr\Recurrence Object
    (
        [start:protected] => DateTime Object
            (
                [date] => 2014-08-04 15:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

        [end:protected] => DateTime Object
            (
                [date] => 2014-08-04 17:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

    )

  )

1 个答案:

答案 0 :(得分:2)

这在此上下文中不起作用 - 您可以使用foreach循环来遍历元素,但不允许您访问对象的属性,因为它被标记为受保护。因此,您将使用可以访问它的Getter。

foreach (<yourarray> as $numObject => $object)
{
    $object->end;  // So you could access it, but its protected
    $object->getEndDate();  // Like this you can access it
}

如果你拥有它,那么你拥有一个简单的\ DateTime对象和format method,你可以得到你的日期字符串,例如$object->getEndDate()->format('Y-m-d H:i:s');