简体   繁体   English

一个键的JSON和值,其中有更多具有相同名称的键的PHP

[英]JSON sum values of a key where there are more keys with the same name PHP

I am playing with some air quality data where each results contains at least 2 results. 我正在播放一些空气质量数据,其中每个结果包含至少2个结果。 I know that I can limit the loop and get the data from the last entry only, but would have been very nice if I can actually sum the values of the same keys and divide the result by the number of entries, so I can get more precise value. 我知道我可以限制循环并仅从最后一个条目获取数据,但如果我能够实际求和相同键的值并将结果除以条目数,那将会非常好,所以我可以得到更多准确的价值。 在此输入图像描述

A problem that I see there is that the key value for P1 and P2 are within the same array so no idea how to sum the two keys separately 我看到的问题是P1和P2的键值在同一个数组内,所以不知道如何将两个键分别相加

The difference between the values in two sets of P1 and P2 keys is coming from the timestamp of the data. 两组P1和P2键中的值之间的差异来自数据的时间戳。

Any idea how can I achieve that? 任何想法我怎么能实现这一目标?

Here is the JSON data 这是JSON数据

[{"sensor":{"sensor_type":{"name":"SDS011","manufacturer":"Nova Fitness","id":14},"pin":"1","id":19770},"sensordatavalues":[{"value_type":"P1","value":"250.80","id":6323692330},{"value_type":"P2","value":"68.70","id":6323692332}],"location":{"country":"BG","latitude":"41.9460","altitude":"229.5","longitude":"25.5380","id":10044},"sampling_rate":null,"timestamp":"2019-02-26 14:02:34","id":2977869281},{"sensor":{"sensor_type":{"name":"SDS011","manufacturer":"Nova Fitness","id":14},"pin":"1","id":19770},"sensordatavalues":[{"value_type":"P1","value":"106.87","id":6323721400},{"value_type":"P2","value":"34.80","id":6323721402}],"location":{"country":"BG","latitude":"41.9460","altitude":"229.5","longitude":"25.5380","id":10044},"sampling_rate":null,"timestamp":"2019-02-26 14:05:04","id":2977883059}]

This is one way, using 2 foreach loops and some variable variables. 这是一种方法,使用2个foreach循环和一些变量变量。

$json = '[{"sensor":{"sensor_type":{"name":"SDS011","manufacturer":"Nova Fitness","id":14},"pin":"1","id":19770},"sensordatavalues":[{"value_type":"P1","value":"250.80","id":6323692330},{"value_type":"P2","value":"68.70","id":6323692332}],"location":{"country":"BG","latitude":"41.9460","altitude":"229.5","longitude":"25.5380","id":10044},"sampling_rate":null,"timestamp":"2019-02-26 14:02:34","id":2977869281},{"sensor":{"sensor_type":{"name":"SDS011","manufacturer":"Nova Fitness","id":14},"pin":"1","id":19770},"sensordatavalues":[{"value_type":"P1","value":"106.87","id":6323721400},{"value_type":"P2","value":"34.80","id":6323721402}],"location":{"country":"BG","latitude":"41.9460","altitude":"229.5","longitude":"25.5380","id":10044},"sampling_rate":null,"timestamp":"2019-02-26 14:05:04","id":2977883059}]';
$array = json_decode($json);

$P1 = 0;
$P2 = 0;
$P1C = 0;
$P2C = 0;
foreach ($array as $arr) {
    foreach ($arr->sensordatavalues as $obj) {
        ${$obj->value_type} += $obj->value;
        ${$obj->value_type.'C'}++;
    }
}
echo "P1 = $P1 and P2 = $P2\n"; 
echo "P1C = $P1C and P2C = $P2C\n"; 
echo "Last timestamp = " . $array[count($array)-1]->timestamp;

Output: 输出:

P1 = 357.67 and P2 = 103.5
P1C = 2 and P2C = 2
Last timestamp = 2019-02-26 14:05:04

You can use $.each method in jQuery and calculate the array values. 您可以在jQuery中使用$.each方法并计算数组值。

 var jsonURL = "https://api.jsonbin.io/b/5c755697f90f2c3f31aa1d6b"; var sumP1 = 0 var sumP2 = 0; var countP1 = 0; var countP2 = 0; $.getJSON(jsonURL, function (result) { $.each(result, function (i, field) { var sensordatavalues = field.sensordatavalues; sensordatavalues.forEach(data => { if (data.value_type == "P1") { countP1++; sumP1 += Number(data.value); } if (data.value_type == "P2") { countP2++; sumP2 += Number(data.value); } }); }); console.log("sumP1: " + sumP1 / countP1); console.log("countP1: " + countP1); console.log("-------------------"); console.log("sumP2: " + sumP2 / countP2); console.log("countP2: " + countP2); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM