简体   繁体   中英

2D PHP array, join values based on similar values

I have PHP array which I use to draw a graph

Json format:

{"y":24.1,"x":"2017-12-04 11:21:25"},

{"y":24.1,"x":"2017-12-04 11:32:25"},

{"y":24.3,"x":"2017-12-04 11:33:30"},

{"y":24.1,"x":"2017-12-04 11:34:25"},

{"y":24.2,"x":"2017-12-04 11:35:35"},.........

{"y":26.2,"x":"2017-12-04 11:36:35"}, ->goes up for about a minute

{"y":26.3,"x":"2017-12-04 11:37:35"},.........

{"y":24.1,"x":"2017-12-04 11:38:25"},

{"y":24.3,"x":"2017-12-04 11:39:30"}

y=is temperature and x value is date time,

as you can see temperature doesn't change so often even if, it change only for max 0.4. But sometimes after a long period of similar values it change for more than 0.4.

I would like to join those similar values, so graph would not have 200k of similar values but only those that are "important".

I would need an advice, how to make or which algorithm would be perfect to create optimized array like i would like.

perfect output:

{"y":24.1,"x":"2017-12-04 11:21:25"},.........

{"y":24.1,"x":"2017-12-04 11:34:25"},

{"y":24.2,"x":"2017-12-04 11:35:35"},.........

{"y":26.2,"x":"2017-12-04 11:36:35"}, ->goes up for about a minute

{"y":26.3,"x":"2017-12-04 11:37:35"},.........

{"y":24.1,"x":"2017-12-04 11:38:25"}

Any help?

As you specified php I'm going to assume you can handle this on the output side.

Basically, you want logic like "if the absolute value of the temperature exceeds the last temperature by so much, or the time is greater than the last time by x minutes, then let's output a point on the graph". If that's the case you can get the result by the following:

$temps = array(); //your data in the question
$temp = 0;
$time = 0;
$time_max = 120; //two minutes
$temp_important = .4; //max you'll tolerate
$output = [];
foreach($temps as $point){
    if(strtotime($point['x']) - $time > $time_max || abs($point['y'] - $temp) >= $temp_important){
        // add it to output
        $output[] = $point;
    }
    //update our data points
    if(strtotime($point['x']) - $time > $time_max){
        $time = strtotime($point['x']);
    }
    if(abs($point['y'] - $temp) >= $temp_important){
        $temp = $point['y'];
    }
}
// and out we go..
echo json_encode($output);

Hmm, that's not exactly what you're asking for, as if the temp spiked in a short time and then went down immediately, you'd need to change your logic - but think of it in terms of requirements.

If you're RECEIVING data on the output side I'd write something in javascript to store these points in/out and use the same logic. You might need to buffer 2-3 points to make your decision. Your logic here is performing an important task so you'd want to encapsulate it and make sure you could specify the parameters easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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