如何访问Foreach内的对象?

时间:2016-08-03 10:11:54

标签: php

我的数组看起来像这样:

Array
(
[id] => 196011470503479_873596956078257
[message] => #Dabur India Ltd has been ranked amongst India's Super 50 Companies by Forbes India. The annual list ranks companies that generate high returns for investors, grow their Revenues strongly and deploy funds efficiently.
[created_time] => DateTime Object
    (
        [date] => 2016-07-29 04:00:01.000000
        [timezone_type] => 1
        [timezone] => +00:00
    )

[shares] => Array
    (
        [count] => 26
    )

PHP代码

</tbody>
    <?php
        $i = 1;
        foreach($arr['posts'] as $poInd=>$poVal){
            echo "
                <tr>
                    <td>".$i."</td>
                    <td>".$poVal['id']."</td>
                    <td>".$poVal['name']."</td>
                    <td>".$poVal['message']."</td>
                    <td>".($poVal['created_time']->date)."</td>
                    <td>".$poVal['shares']['count']."</td>
                    <td>".count($poVal['likes'])."</td>
                    <td>".count($poVal['comments'])."</td>
                </tr>
            ";
            $i++;
        }
    ?>
</tbody>

我的输出是 enter image description here 在我的表中,第5列是created_time,在我的数组created_time索引中包含DateTime对象。那么如何从DateTime循环中的foreach对象获取日期?

2 个答案:

答案 0 :(得分:5)

created_timeDateTime对象,因此您应该使用它,即:

$poVal['created_time']->format('Y-m-d H:i:s')

答案 1 :(得分:1)

您的数组$poVal['created_time']中有一个日期时间对象。因此,请将format与datetimeobject一起使用

$poVal['created_time']->format('Y-m-d H:i:s');