2014-07-24 20 views
0

我需要渲染一個圖形,但我每2米獲取一個數據。 爲了順利我的渲染,我想完成缺失數據每個奇數米,與 - next和prev關鍵 之間的深度 - - 同日 平均值之間的未來和prev關鍵在陣列的next和prev值之間插入平均值(每奇數)

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

如何添加我需要的值?

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

+0

爲什麼會變成這樣平滑線條的平均?如果您想要平滑要繪製曲線的線條,但仍然需要繪製相同的直線,但需要考慮多個數據點,並且應該生成比原始輸入更多的新點。讓我知道你是否感興趣(基本思路是映射每個現有點的增量值,然後根據會導致的轉換點添加新點,並重復N次,其中N至少爲2(否則它會看起來鋸齒))。 – scragar

回答

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']; });