dpkt17803 2014-11-07 04:00
浏览 46
已采纳

在Doctrine arraycollection中使用foreach循环

I have just discovered the following library https://github.com/simshaun/recurr and the output of is using a Doctrine arrayCollection.

How can I use a foreach loop to loop through this array and get the date value?

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条回答 默认 最新

  • dongyi1159 2014-11-07 04:22
    关注

    This will not work in this context - you can use a foreach loop to get through the elements, but you are not allowed to access the property of the object, because it's marked as protected. So you'll use a Getter that you can access it.

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

    And if you have it, than you have a simple \DateTime Object and with the format method you can get your date string e.g. $object->getEndDate()->format('Y-m-d H:i:s');.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部