2D PHP数组,根据相似的值

时间:2018-02-15 10:32:22

标签: php arrays optimization multidimensional-array mathematical-optimization

我有用于绘制图形的PHP数组

Json格式:

{"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 =是温度,x值是日期时间,

正如你所看到的那样,温度并没有经常变化,即使它变化只有最大0.4。但有时经过长时间的类似值后,它的变化超过0.4。

我想加入那些类似的值,因此图表不会有200k的相似值,只有那些重要的值#34;。

我需要一个建议,如何制作或者哪种算法可以完美地创建我喜欢的优化数组。

完美输出:

{"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"}

任何帮助?

1 个答案:

答案 0 :(得分:0)

正如您指定php我假设您可以在输出端处理此问题。

基本上,你想要像#34这样的逻辑;如果温度的绝对值超过最后一个温度那么多,或者时间大于最后一次x分钟,那么让我们输出一个点在图表上#34;。如果是这种情况,您可以通过以下方式获得结果:

$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);
嗯,这不完全是你要求的,好像温度在短时间内飙升然后立即下降,你需要改变你的逻辑 - 但想到它在要求方面。

如果您在输出端接收数据,我会在javascript中写一些内容来存储这些点,并使用相同的逻辑。您可能需要缓冲2-3点才能做出决定。您的逻辑执行一项重要任务,因此您需要对其进行封装,并确保您可以轻松指定参数。

相关问题