在数组的下一个值和前一个值之间插入平均值(每个奇数)


Insert average between next and prev values of array (every odd)

我需要渲染一个图形,但我得到的数据每2米。为了平滑我的渲染,我想完成每奇数米缺失的数据,用-同一天-下一个键和上一个键之间的深度-下一个键和上一个键之间的平均值

My foreach looks like

        foreach ($datas as $data) {
           $chartline[] = array(
              'date' =>  $data->date, 
              'depth' => $data->z, 
              'value' => round($data->value, 2)
           );
        }

var_dump显示:

  0 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 2
      'value' => float 23.45
  1 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 4
      'value' => float 20.48
  2 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 6
      'value' => float 19.76
  3 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 8
      'value' => float 18.78
  4 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 10
      'value' => float 17.9
  5 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 12
      'value' => float 17.04
  6 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 14
      'value' => float 16.71
  7 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 16
      'value' => float 16.25

我想转换成

0 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 1
      'value' => float 23.45
  1 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 2
      'value' => float 23.45
  2 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 3
      'value' => float (AVERAGE BETWEEN 23.45 the previous key AND 20.48 the next key)
  3 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 4
      'value' => float 20.48
  4 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 5
      'value' => float (AVERAGE BETWEEN 20.48 the previous key AND 17.9 the next key)
  5 => 
    array (size=3)
      'date' => string '2014-07-23 14:30:00' (length=19)
      'depth' => float 6
      'value' => float 17.9
.
.
.
.
.

如何添加我需要的值?

如果你有任何想法或提示!

按深度为数组索引,然后为每个奇数获得depth+1和depth-1的平均值

$indexedByDepth = array();
foreach($array as $value) {
    $indexedByDepth[(int) $value['depth']] = $value;
}
foreach(range(1,max(array_keys($indexedByDepth)) as $i) {
    if($i % 2 == 0)
        continue;
    if(isset($indexedByDepth[$i-1]) && isset($indexedByDepth[$i+1])) {
        $average = ($indexedByDepth[$i-1]['value'] + $indexedByDepth[$i+1]['value'])/2;
        $array[] = array(
            'date' => $indexedByDepth[$i-1]['date'],
            'depth' => (float) $i,
            'value' => $average,
        );  
    }
    elseif(isset($indexedByDepth[$i-1])) {
        $array[] = $indexedByDepth[$i-1];   
    }
    elseif(isset($indexedByDepth[$i+1])) {
        $array[] = $indexedByDepth[$i+1];
    }
}
usort($array,function($a,$b) { return $a['depth'] - $b['depth']; });