循环中的多维数组前置一个0然后是两个0然后是树0等

时间:2014-01-15 20:18:00

标签: php arrays multidimensional-array

我有点坚持这几个小时。我需要每次循环,循环几周向多维数组添加额外的“0”。

例如,这就是我想要实现的目标

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 个答案:

答案 0 :(得分:2)

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

编辑:更改&lt; =到&lt;在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...
    */

在回答以下评论时:以下内容将使用周数据的总和填充数组,其中每个连续索引包含前几周所有数据的总和。

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

答案 1 :(得分: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 />";

        }
    }