我刚刚发现了以下图书馆 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
)
)
)
最佳答案
这在此上下文中不起作用 - 您可以使用 foreach 循环遍历元素,但不允许访问对象的属性,因为它被标记为 protected 。因此,您将使用可以访问它的 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');
.
关于php - 在 Doctrine 数组集合中使用 foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800824/