在数组的next和prev值之间插入平均值(每个奇数)

时间:2014-07-24 14:18:39

标签: php arrays

我需要渲染一个图表,但我每隔2米就得到一个数据。 为了平滑我的渲染,我希望每个奇数米完成丢失的数据 - 同一天 - next和prev键之间的深度 - next和prev key之间的平均值

我的foreach看起来像

        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
.
.
.
.
.

如何添加我需要的值?

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

1 个答案:

答案 0 :(得分:1)

按深度索引数组,然后为每个奇数得到平均深度+ 1和深度-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']; });