Php 如何访问Foreach内部的对象?
我的数组如下所示:Php 如何访问Foreach内部的对象?,php,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 Rev
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>
我的输出是
在我的表中,第5列是
created\u time
,在我的数组created\u time
索引中包含一个DateTime
对象。那么如何从foreach
循环中的DateTime
对象中获取日期呢?创建的\u time
是一个DateTime
对象,因此您应该使用它,即:
$poVal['created_time']->format('Y-m-d H:i:s')
数组中有一个datetime对象
$poVal['created\u time']
。所以使用datetimeobject
$poVal['created_time']->格式('Y-m-dh:i:s')代码>这个答案与@haz
有什么不同??