4
“任何” MongoDB的结果键我有一个PHP脚本,返回CSV格式的结果:如何从对象
# cat query.php
<?php
include '../misc.php';
$values=$_GET?$_GET:$_POST;
{
$ts = isset($values['t'])?strtotime($values['t']):strtotime("-1 hour");
$result=Query('iot.data',['key' => $values['key'], 'timestamp' => array('$gte' => $ts)],['projection' => array('_id' => 0, 'timestamp' => 1, $values['col'] => 1), 'sort' => array('timestamp' => 1)]);
if ($values['col'] == 'temp1') echo "Timestamp, Temperature\n";
if ($values['col'] == 'BusV2') echo "Timestamp, Battery V\n";
if ($values['col'] == 'uv1') echo "Timestamp, UV\n";
foreach ($result as $r) {
$d = date('c',$r->timestamp);
if (isset($r->temp1)) echo "$d, $r->temp1\n";
if (isset($r->BusV2)) echo "$d, $r->BusV2\n";
if (isset($r->uv1)) echo "$d, $r->uv1\n";
};
};
随着“山坳”参数我可以决定与时间戳返回了什么键。
如何在foreach循环中没有这些IF的情况下获得理想的结果?
如何将我的代码是这样的:如果我改变的foreach到
foreach ($result as $r) {
$d = date('c',$r->timestamp);
echo "$d, $r->$values['col']\n" // where object $r key comes from variable 'col'
}
它没有帮助: foreach ($result as $r => $v) {
因为$v
没有得到关键$r
值,$v
是现在什么$r
之前...
结果print_r($v);
stdClass Object
(
[temp1] => 25.2
[timestamp] => 1470304854
)
不要工作!当php点击'$ r [$ values ['col']]'时,它返回(到http服务器)错误500.当php尝试解释'$ r - > $ values ['col']'时,它不会给出错误,但只是停止做..没有线在该点后执行。 mongo-driver返回的结构是特殊的东西..我想。 – JJussi
当你把它放在$ d = date('c',$ r-> timestamp)之前时,'print_r($ r)'返回什么?'在你的foreach中? –
stdClass的对象 ( [humidity1] => 68.9 [时间戳] => 1470391190 ) 2016-08-05T12:59:50 + 03:00,68.9 – JJussi