4

I am playing with some air quality data where each results contains at least 2 results. 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.enter image description here

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

The difference between the values in two sets of P1 and P2 keys is coming from the timestamp of the data.

Any idea how can I achieve that?

Here is the JSON data

[{"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}]
4
  • You want to sum the same Key values right ? Commented Feb 26, 2019 at 15:08
  • yes... P2 values and P1 Values and then divide them to number of results Commented Feb 26, 2019 at 15:09
  • Refer the answer and let me know stackoverflow.com/questions/54888438/… Commented Feb 26, 2019 at 15:21
  • is it working in your case Commented Feb 26, 2019 at 15:27

2 Answers 2

2

This is one way, using 2 foreach loops and some variable variables.

$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
Sign up to request clarification or add additional context in comments.

4 Comments

Sound good, but how do you divide each sum on the number of results?
Ahh didnt see that in the comment. Check now I added a counter as well for each val
Yeap, it works great. How can I get the last timestamp?
There you go, the last timestamp
1

You can use $.each method in jQuery and calculate the array values.

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>

1 Comment

Yeap, that would work as well. But I needed a PHP way to do it. Nevertheless it is very good to have two different approaches to solve this case. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.