我试图使用PHP解码来解析我的JSON字符串到一个数组中,这样我就可以从文件中提取两个通道的当前日期。JSON到PHP数组问题
我的JSON文件owl_output.json样子..
{"channels":{"0":[{"current":1288,"units":"w"},{"day":31278.57,"units":"wh"}],"1": [{"current":660,"units":"w"},{"day":9191.11,"units":"wh"}]}}
我'永远只能得到一个结果显示,PHP代码我设法得到工作低于
<?php
$string = file_get_contents('owl_output.json');
$data = json_decode($string,true);
print_r($json);
foreach ($data['channels']['0'] as $data)
{
echo $data ['current'];
}
?>
这只显示通道0的当前电流。如果我尝试添加其他字段,则不会显示
echo $ data ['current'] ['day']; (不起作用)
有人可以告诉我如何显示当前和这两个频道的日期0 & 1?
我的目标是在最后的html页面中显示此内容并继续轮询json文件?
它显示阵列低于
Array
(
[channels] => Array
(
[0] => Array
(
[0] => Array
(
[current] => 1288
[units] => w
)
[1] => Array
(
[day] => 31278.57
[units] => wh
)
)
[1] => Array
(
[0] => Array
(
[current] => 660
[units] => w
)
[1] => Array
(
[day] => 9191.11
[units] => wh
)
)
)
)
任何人都可以提供任何与此帮助?
感谢
您应该遍历'$数据[ '渠道']','不是$数据[ '渠道'] [0]'。 –