一个键的JSON总和值,其中有更多同名的键

时间:2019-02-26 15:02:33

标签: php arrays json

我正在处理一些空气质量数据,其中每个结果至少包含2个结果。我知道我可以限制循环并仅从最后一个条目中获取数据,但是如果我实际上可以对相同键的值求和并将结果除以条目数,那将是非常不错的,因此我可以获得更多精确值。enter image description here

我看到的一个问题是P1和P2的键值在同一数组中,所以不知道如何分别将两个键相加

两组P1和P2键中的值之间的差异来自数据的时间戳。

任何想法我该如何实现?

这是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}]

2 个答案:

答案 0 :(得分:2)

这是一种方法,使用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;

输出:

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

答案 1 :(得分:1)

您可以在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>