php - 如何从 url 获取数据到数组?
问题描述
我用 php 很糟糕。我不是只为我的私人事情做点什么的程序员。我有这样一个问题,我想从我的 PV 产品中下载数据。数据格式如下。如何分别从 php 中的 url 下载数据,对它们进行分组并将它们发送到数组?预先感谢您的所有帮助。
{"sid":62923,"dataunit":"kWh","data":[{"time":"2019-08-01","no":"1","value":"27.7"}, {"time":"2019-08-02","no":"2","value":"24.0"},{"time":"2019-08-03","no":"3" ,"值":"19.9"},{"时间":"2019-08-04","否":"4","值":"25.3"},{"时间":"2019-08- 05","no":"5","value":"0.0"},{"time":"2019-08-06","no":"6","value":"0.0"}, {"time":"2019-08-07","no":"7","value":"0.0"},{"time":"2019-08-08","no":"8","value":"0.0"},{"time":"2019-08-09","no":"9","value":"0.0"},{"time ":"2019-08-10","no":"10","value":"0.0"},{"time":"2019-08-11","no":"11","value ":"0.0"},{"time":"2019-08-12","no":"12","value":"0.0"},{"time":"2019-08-13", "no":"13","value":"0.0"},{"time":"2019-08-14","no":"14","value":"0.0"},{"time ":"2019-08-15","no":"15","value":"0.0"},{"time":"2019-08-16","no":"16","value":"0.0"},{"time":"2019-08-17","no":"17","value":"0.0"},{"time":" 2019-08-18","no":"18","value":"0.0"},{"time":"2019-08-19","no":"19","value":" 0.0"},{"time":"2019-08-20","no":"20","value":"0.0"},{"time":"2019-08-21","no" :"21","value":"0.0"},{"time":"2019-08-22","no":"22","value":"0.0"},{"time":" 2019-08-23","no":"23","value":"0.0"},{"time":"2019-08-24","no":"24","value":"0.0"},{"time":"2019-08-25","no":"25","value":"0.0"},{"time":"2019- 08-26","no":"26","value":"0.0"},{"time":"2019-08-27","no":"27","value":"0.0" },{"time":"2019-08-28","no":"28","value":"0.0"},{"time":"2019-08-29","no":" 29","value":"0.0"},{"time":"2019-08-30","no":"30","value":"0.0"},{"time":"2019- 08-31","否":"31","值":"0.0"}]}2019-08-25","no":"25","value":"0.0"},{"time":"2019-08-26","no":"26","value":" 0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019-08-28","no" :"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0"},{"time":" 2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":"31","value":" 0.0"}]}2019-08-25","no":"25","value":"0.0"},{"time":"2019-08-26","no":"26","value":" 0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019-08-28","no" :"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0"},{"time":" 2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":"31","value":" 0.0"}]}26","value":"0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019- 08-28","no":"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0" },{"time":"2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":" 31","值":"0.0"}]}26","value":"0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019- 08-28","no":"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0" },{"time":"2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":" 31","值":"0.0"}]}2019-08-29","no":"29","value":"0.0"},{"time":"2019-08-30","no":"30","value":" 0.0"},{"time":"2019-08-31","no":"31","value":"0.0"}]}2019-08-29","no":"29","value":"0.0"},{"time":"2019-08-30","no":"30","value":" 0.0"},{"time":"2019-08-31","no":"31","value":"0.0"}]}
解决方案
试试这个
<?php
$json_url = file_get_contents('Your url goes here');
$data = json_decode($json_url,true);
var_dump($data);
// You can print all of your data here to see if it works
foreach ($data as $items) {
// You can loop trough the data and get it like $items->sid etc
var_dump($items);
}
?>
您需要使用“ file_get_contents ”获取 URL,而不是使用json_decode对其进行解码
然后用 for each 函数打印出来
推荐阅读
- python - 如果它以前有效,为什么这不起作用?嵌入消息不根据角色发送
- javascript - 如何将 .NET 应用程序中的 JS 注入 Shopify 的动态主题?
- automator - 执行剪贴板内容的全局热键
- python - 使用 readlines() 读取多个打开的文件会导致空数组
- javascript - 如何在不使用jquery的情况下获取另一个html文件,就像通过文档获取它一样
- javascript - 无法在 Mocha 框架 v8.4.0 上访问 foreach 内的全局变量
- python - 无法同时更改所有树视图表中的参数 - python
- blazor - MudBlazor 的 MudChip 默认 true 不起作用
- r - 如何更改 R 中 ggplot (geom_col) 的 Y 范围?
- python - Python3“math.sin”函数返回不正确的结果(角度以弧度为单位。)