替换php数组中的异常数字



我在php中有一个数字数组,就像下面的一样

[2021-06-04] => Array
(
[09:15:04] => 1.51
[09:15:27] => 1.32
[09:15:46] => 14.29
[09:16:05] => 14.03
[09:18:05] => 13.97
[09:18:24] => 13.88
[09:18:42] => 13.94
[09:19:01] => 13.80
[09:19:05] => 13.86
[09:19:23] => 13.97
[09:19:41] => 6.99
[09:20:00] => 14.14
[09:20:06] => 14.08
[09:20:25] => 14.04
)

在以上数组项中1.51,1.32和6.99有些偏离,所以我想用下一个元素替换then,这样它的结果数组应该如下所示。

[2021-06-04] => Array
(
[09:15:04] => 14.29
[09:15:27] => 14.29
[09:15:46] => 14.29
[09:16:05] => 14.03
[09:18:05] => 13.97
[09:18:24] => 13.88
[09:18:42] => 13.94
[09:19:01] => 13.80
[09:19:05] => 13.86
[09:19:23] => 13.97
[09:19:41] => 14.14
[09:20:00] => 14.14
[09:20:06] => 14.08
[09:20:25] => 14.04
)

我可以使用获得阵列平均值

$a = array_filter($a);
$average = array_sum($a)/count($a);
echo $average;

我试了一些低于的东西

foreach($arr as $key=>$val)
{
if($avg-$val) > 5)
{
$arr[$key]  = $nextactualelem; // How do I get next correct element whose $avg-$val is less than 5

}
}

附言:嗨,有可能得到下10个元素的平均值吗。。而不是总平均值。。因为在更大的集合中它不起作用。。因为范围从10到30的较大数据集…所以5的差对初始较小的值不起作用。或者有其他更快的方法可以做到这一点,比如下一个和上一个数字之间的差。。

如果要更改正在处理的数组,foreach不是您的最佳选择。引用键很麻烦,每次运行时变量值都不干净。通常,如果需要对正在迭代的数组执行某些操作,请使用for循环。

现在,由于您要替换这些值,我们有一个小问题。查看边缘案例。如果我们想反向遍历数组(这是有道理的,因为我们想用下一个值替换异常(,但最后一个值是异常,该怎么办?仍然反向顺序是一个不错的选择,但我们需要确保至少最后一个值是有效的。

考虑到键是任意的,我们现在不能使用for循环。值得庆幸的是,我们可以使用内部数组指针四处移动。

$arr = [
'09:15:04' => 1.51,
'09:15:27' => 1.32,
'09:15:46' => 14.29,
'09:16:05' => 14.03,
'09:18:05' => 13.97,
'09:18:24' => 13.88,
'09:18:42' => 13.94,
'09:19:01' => 13.80,
'09:19:05' => 13.86,
'09:19:23' => 13.97,
'09:19:41' => 6.99,
'09:20:00' => 14.14,
'09:20:06' => 14.08,
'09:20:25' => 14.04
];
$average = array_sum($arr)/count($arr);
end($arr); // We move the pointer to the end of the array;
//... do some verification magic here
// so we have a valid starting value on the end of the array
while(prev($arr) != false) { // move one step back while we have values
if (($average - current($arr)) > 5) {
$key = key($arr);           // Grab the current key
$arr[$key] = next($arr);    // replace the value with the next
prev($arr);                 // put the pointer back in the right place
}
}
var_dump($arr);

请注意,如果运行此操作,6.99将保留在数组中,因为它距离平均值只有4.71。因此,对你的条件进行一些调整可能是合理的。

编辑:

获取新信息我们可以尝试以下方式

$lookup[] = end($arr); // We move the pointer to the end of the array and place it in a lookup array
//... do some varification magic here so we have a valid starting value on the end of the array
while(prev($arr) !== false) { // move one step back while we have values
$average = array_sum($lookup)/count($lookup); // calculate the current average based on lookup
if (abs($average - current($arr)) > 2) { // if the difference from the current average is more the 2 points
$arr[key($arr)] = next($arr);   // replace the value with the next
prev($arr);                 // put the pointer back in the right place
}
$lookup[] = current($arr); // add the current value to the average
if (count($lookup) > 10) { // if we have more then 10 values in the lookup
array_shift($lookup); // Shift an element off the beginning of array
}
}

这应该会使事情顺利进行;(

循环遍历数组的反向,保存最后一个值,并将奇数值替换为之前保存的最后值。

更新:现在$avg是接下来10个可用值的平均值。


$arr = [
'09:15:04' => 1.51,
'09:15:27' => 1.32,
'09:15:46' => 14.29,
'09:16:05' => 14.03,
'09:18:05' => 13.97,
'09:18:24' => 13.88,
'09:18:42' => 13.94,
'09:19:01' => 13.80,
'09:19:05' => 13.86,
'09:19:23' => 13.97,
'09:19:41' => 6.99,
'09:20:00' => 14.14,
'09:20:06' => 14.08,
'09:20:25' => 14.04,
];
$avg = 0;
$inverseArr = array_reverse($arr);
$lastValue = false;
$offset = -1; 
foreach ($inverseArr as $key => $val) {
if (($avg - $val) > 5 && $lastValue !== false) {
$arr[$key] = $lastValue;
}
else {
$lastValue = $val;    
}
$nextTen = array_slice($arr, $offset, 10);
$avg = array_sum($nextTen) / count($nextTen);
$offset--;
}
// array (size=14)
//   '09:15:04' => float 14.29
//   '09:15:27' => float 14.29
//   '09:15:46' => float 14.29
//   '09:16:05' => float 14.03
//   '09:18:05' => float 13.97
//   '09:18:24' => float 13.88
//   '09:18:42' => float 13.94
//   '09:19:01' => float 13.8
//   '09:19:05' => float 13.86
//   '09:19:23' => float 13.97
//   '09:19:41' => float 14.14
//   '09:20:00' => float 14.14
//   '09:20:06' => float 14.08
//   '09:20:25' => float 14.04

最新更新