大佬教程收集整理的这篇文章主要介绍了如何使用来自 json,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 Json 文件,其中包含每小时预测的气温,直到一天结束,我正在通过执行以下操作来收集这些值:
(...)
// $rawData is the file with Json
$decoded = Json_decode($rawData,truE);
// $@R_429_10586@l_hours is the number of hours till the end of day
for($k=0;$k<$@R_429_10586@l_hours;$k++){
//get air_temp for each hour till the end of day
$air_temp[$k]=$decoded['timeserIEs'][$k]['air_temperature'];
(...)
我得到了所有的 $air_temp[$k],一切都很好。 我现在需要的是创建一个数组,以从所有收集的 $air_temp[$k] 值中计算 $max、$min 和 $avg 温度等值。
我可以通过做这样的事情来创建一个数组:
$array= [$air_temp[0],$air_temp[1],$air_temp[2],$air_temp[3]]; // echo example [12,10,15,13]
$max_temp=(max($array)); // echoes 15
$min_temp=(min($array)); // echoes 10
(...)
但问题是 [$k] 依赖于 $@R_429_10586@l_hours 它不是一个固定值,所以这个选项没有用。
我的问题是:有没有办法创建一个数组,我可以在其中包含所有 $air_temp[$k] 值,以便进一步处理,例如计算数组的最大值、最小值和平均值?
这是我找到的使用 foreach 创建数组的解决方案。
// create array
$hour =date('H'); //current hour
$hours_left= (23-$hour); //hours left till end of the day
$i = 0;
$items = array();
foreach ($air_temp as $temp) {
$items[] = $temp;
if(++$i > $hours_left) break;
}
print_r($items);
echo(min($items));
echo(max($items));
以上是大佬教程为你收集整理的如何使用来自 json全部内容,希望文章能够帮你解决如何使用来自 json所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。