0

我想通过 PHP使用Rickshaw显示 MySQL 的最后数据

它需要看起来像这样:

[
    {
        "data": [ { "x": 1350995278, "y": 26.12 }, 
                  { "x": 1350995276, "y": 26.19 }, 
                  { "x": 1350995273, "y": 26.12 }, 
                  { "x": 1350995271, "y": 26.19 }, 
                  { "x": 1350995268, "y": 26.19 } ]
    }
]

但我的 PHP with json_encodefunction 返回它有点不同。我应该如何解决?

PHP代码:

    while($row = mysql_fetch_assoc($select)) {
        $r[] = $row;
        $items[] = array('x' => $row['unixtime'], 'y' => $row['sensor']);
    }
    print json_encode($items);

PHP 输出:

[{"x":"1350996886","y":"26.06"},
 {"x":"1350996884","y":"26.06"},
 {"x":"1350996881","y":"26.06"},
 {"x":"1350996879","y":"26.06"},
 {"x":"1350996876","y":"26.06"},
 {"x":"1350996874","y":"26.06"},
 {"x":"1350996871","y":"26.06"},
 {"x":"1350996869","y":"26.06"},
 {"x":"1350996866","y":"26.06"},
 {"x":"1350996864","y":"26.06"}]

我不需要""之后x:和Y:我是新手,PHP不知道如何弄清楚,请指教。

4

1 回答 1

0

如果您不希望引用它们,请将值转换为数字:

$items[] = array('x' => (float) $row['unixtime'], 'y' => (float) $row['sensor']);
于 2012-10-23T16:10:23.457 回答