2015-08-14 108 views
0

我通過ajax獲取我的flot圖的值。之後填充缺失值的數組

在後端腳本我的測試陣列看起來像這樣:

Array 
(
    [0] => Array 
     (
      [0] => 6 
      [1] => 1 
     ) 

    [1] => Array 
     (
      [0] => 7 
      [1] => 7 
     ) 

    [2] => Array 
     (
      [0] => 8 
      [1] => 37 
     ) 

    [3] => Array 
     (
      [0] => 9 
      [1] => 44 
     ) 
) 

值與偏移[0]表示的小時。 現在我每小時需要24個數組對象。如何在不觸及給定元素的情況下保持這一點?

例如

Array 
(
    [0] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [6] => Array 
     (
      [0] => 6 
      [1] => 1 
     ) 
    [...] 
    [7] => Array 
     (
      [0] => 7 
      [1] => 7 
     ) 

    [8] => Array 
     (
      [0] => 8 
      [1] => 37 
     ) 

    [9] => Array 
     (
      [0] => 9 
      [1] => 44 
     ) 

在此先感謝。

+0

那麼,每個子陣列中的第一個元素是小時?你是否用SQL查詢拉出這些數據?可能效率更高一些 - 例如你正在使用PDO,你可以用很多功能來改變結果集,比如'$ stmt-> fetchAll(PDO :: FETCH_FUNC,function($ a,$ b ){/ * ... * /});' –

回答

0

解決方案對我來說:

while ($stmt->fetch()) { 
    $x = $hour; 
    $y = $zugriffe; 
    $data1[$x] = array ($x, $y); 
} 

for ($i=0;$i<=24;$i++){ 
    if (!isset($data1[$i])){ 
     $data1[$i] = array ($i,0); 
    } 
} 
sort($data1); 
0

假設數組的名稱爲$一個,你可以這樣做:

for($i = 0; $i < 24; $i++) { 
    if(isset($a[$i])) continue; 
    else { 
     $a[$i] = array($i,"something you like here"); 
    } 
} 
0

你要根據第一子陣列值「重新鍵」這個結果集,然後將其合併到一個數組24小時元素。

嘗試這種情況:

$data = [ 
    [6, 1], 
    [7, 7], 
    [8, 37], 
    [9, 44] 
]; 

$hours = array_fill(0, 25, [0, 0]); 
$data = array_combine(array_column($data, 0), $data); 
$hours = array_replace($hours, $data); 

print_r($hours); 

此:

  1. 創建包含包含默認值[0, 0]
  2. 重新鍵您$data,拉出並使用子陣列24元件陣列$hours每個子數組的第一個值就是這樣做的。這假定你正在使用PHP 5.5。
  3. 最後,更換改良$data$hours

這產生了:

Array 
(
    [0] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [1] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [2] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [3] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [4] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [5] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [6] => Array 
     (
      [0] => 6 
      [1] => 1 
     ) 

    [7] => Array 
     (
      [0] => 7 
      [1] => 7 
     ) 

    [8] => Array 
     (
      [0] => 8 
      [1] => 37 
     ) 

    [9] => Array 
     (
      [0] => 9 
      [1] => 44 
     ) 

    [10] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [11] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [12] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [13] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [14] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [15] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [16] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [17] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [18] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [19] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [20] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [21] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [22] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [23] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

) 

希望這有助於:)

+0

如果您沒有使用PHP 5.5,請查看[array_column()]的PHP實現(https://github.com/ramsey/array_column/blob/ master/src/array_column.php)H/T到@AbraCadaver –

0

你可以試試還有這個:

// yours test array 
    $times = array(
     array(6,1), 
     array(7,7), 
     array(8,37), 
     array(9,44) 
    ); 

    // array with all hours 
    $fullTimes = array_fill(0, 24, array(0,0)); 

    foreach ($times as $time) { 
     $fullTimes[$time[0]] = $time; 
    }