2014-01-15 83 views
0

我有點卡在這個幾個小時。我需要每一次循環,在幾周內向多維數組添加一個額外的「0」。在一個循環中的多維數組預先一個0然後兩個0的然後樹0的等

例如這是我想achievev

Array 
(
    [week1] => Array // Week 1 do nothing 
     (
      [0] => 0.461718 
      [1] => 2.874501 
      [2] => 4.968576 
      [3] => 4.353633 
      [4] => 3.019554 
      [5] => 2.026656 
      [6] => 1.405584 
      [7] => 1.119564 
      [8] => 1.131822 
     ) 

    [week2] => Array // Week 2 prepend one "0" 
     (
      [0] => 0 
      [1] => 0.461718 
      [2] => 2.874501 
      [3] => 4.968576 
      [4] => 4.353633 
      [5] => 3.019554 
      [6] => 2.026656 
      [7] => 1.405584 
      [8] => 1.119564 
      [9] => 1.131822 
     ) 

    [week3] => Array // Week 3 prepend two "0's" 
     (
      [0] => 0 
      [1] => 0 
      [2] => 0.461718 
      [3] => 2.874501 
      [4] => 4.968576 
      [5] => 4.353633 
      [6] => 3.019554 
      [7] => 2.026656 
      [8] => 1.405584 
      [9] => 1.119564 
      [10] => 1.131822 
     ) 

    [week3] => Array // Week 4 prepend three "0's" 
     (
      [0] => 0 
      [1] => 0 
      [2] => 0 
      [3] => 0.461718 
      [4] => 2.874501 
      [5] => 4.968576 
      [6] => 4.353633 
      [7] => 3.019554 
      [8] => 2.026656 
      [9] => 1.405584 
      [10] => 1.119564 
      [11] => 1.131822 
     ) 

     // Etc... 

這是我的代碼。 我現在得到每陣列僅一個 「0」,在預先

$totaal = array(); 

    for($w = 1; $w <= 52; $w++) 
    { 
     for($sw = 0; $sw <= 8; $sw++) 
     {   
      $totaal["week".$w][] = $vruchtzettings_week["week".$w][$sw]; 
     } 
    } 

    for($w = 1; $w <= 52; $w++) 
    { 
     $sum = 0; 

     array_unshift($totaal["week".$w], $sum); 

     for($sw = 0; $sw <= 8; $sw++) 
     {   
      echo $totaal["week".$w][$sw]."<br />"; 

     } 
    } 

由於

回答

2
$increments = 0; 
foreach ($weeks as &$week) { 
    if ($increments > 0) { 
     for ($i=0; $i< $increments; $i++) { 
      array_unshift($week, 0); 
     } 
    } 
    $increments++; 
} 

編輯:在深度響應於已改變的< =到< for循環

更多:

// If I'm not mistaken $vruchtzettings_week contains all your 
// data up to 52 weeks. You're first transferring this data 
// to a variable, $totaal, by looping through it's contents. But 
// why not just set $totaal equal to $vruchtzettings_week? 
$totaal = $vruchtzettings_week; // tada! 

$increments = 0; 
foreach ($totaal as &$data) { // We make a reference of $data so any changes are preserved 
    if ($increments > 0) { 
     for ($i=0; $i< $increments; $i++) { 
      array_unshift($data, 0); 
     } 
    } 
    $increments++; 
} 

var_dump($totaal); // Total now looks like what you wanted 

/* 
Array 
(
[week1] => Array // Week 1 do nothing 
    (
     [0] => 0.461718 
     [1] => 2.874501 
     [2] => 4.968576 
     [3] => 4.353633 
     [4] => 3.019554 
     [5] => 2.026656 
     [6] => 1.405584 
     [7] => 1.119564 
     [8] => 1.131822 
    ) 

[week2] => Array // Week 2 prepend one "0" 
    (
     [0] => 0 
     [1] => 0.461718 
     [2] => 2.874501 
     [3] => 4.968576 
     [4] => 4.353633 
     [5] => 3.019554 
     [6] => 2.026656 
     [7] => 1.405584 
     [8] => 1.119564 
     [9] => 1.131822 
    ) 
    etc... 
    */ 

在回答下面的評論:foll欠款將填入週數據總和,其中每個連續的指數包括所有星期前的數據總和。

$totaal_part1 = array(); 
foreach ($totaal as $i => $data) { 
    $totaal_part1[] = array_sum($data) + (isset($totaal_part1[$i - 1]) ? $totaal_part1[$i - 1] : 0); 
} 
+0

你能告訴我,我的例子請,我不得到它適合togetter – Julez

+0

看起來是這樣工作的:D現在我需要計算例如$ totaal_part1 [1] =第1周,$ totaal_part1 [2] =第1周+第2周,$ totaal_part1 [3] =第1周+第2周+第3周等等...最多52次最好的方法來做到這一點? – Julez

+0

謝謝你,你好!我不明白你所做的一切,但我會研究它,並試圖瞭解 – Julez

2

這是不是很優雅,但應該工作:

$totaal = array(); 

    for($w = 1; $w <= 52; $w++) 
    { 
     for($sw = 0; $sw <= 8; $sw++) 
     {   
      $totaal["week".$w][] = $vruchtzettings_week["week".$w][$sw]; 
     } 
    } 

    for($w = 1; $w <= 52; $w++) 
    { 
     $sum = 0; 

     for ($i = 0; $i < $w; $i++) array_unshift($totaal["week".$w], $sum); 

     for($sw = 0; $sw <= 8; $sw++) 
     {   
      echo $totaal["week".$w][$sw]."<br />"; 

     } 
    } 
+0

你解決方案取代現有的價值,我需要他們prepend和保持現有的 – Julez

+0

自己測試它,並像預期的一樣工作... –

+0

是的你是正確的,即時通訊抱歉 – Julez

相關問題