更改for循环中的增量值

时间:2016-03-22 15:18:59

标签: php arrays loops for-loop increment

我正在尝试为$ matchno设置正确的值。 它应该仅增加1。下面是输出数组的代码和示例,我手动更改了数组,以显示我希望数组如何。关注的只有$ matchno

$max = 16;
$tournament_size = $max / 2;
$rounds = log($tournament_size) / log(2);

$curr = $tournament_size / 2;

$offset = $max;
$matchnoInBlock = -1;

for ($i = 0; $i < $rounds; $i++) 
{
    $inner = array();

    for ($i2 = 0; $i2 < $curr; $i2++)
    {
        $matchnoInBlock++;
        $matchno = $matchnoInBlock + $offset;

        $inner[] = array(0, 0, $matchno);
    }

    $lower_bracket_results[] = $inner;
    $lower_bracket_results[] = $inner;

    $curr /= 2;
}

echo '<pre>';
print_r($lower_bracket_results);
echo '</pre>';

阵列输出: -

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

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

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

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

        )

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

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

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

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

        )

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

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

        )

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

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

        )

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

        )

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

        )

)

我希望数组是这样的:(唯一不同的是$ matchno)

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

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

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

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

        )

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

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

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

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

        )

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

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

        )

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

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

        )

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

        )

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

        )

)

对此有任何帮助,谢谢

1 个答案:

答案 0 :(得分:1)

如果我的问题正确无误,您只需在将其分配给数组后再增加$matchno

$matchno = 16;

for($x = 0; $x <= 3; $x++)
{
    $lower_bracket_results[0][$x][2] = $matchno;
    $matchno++;
}

或以较短的方式

$matchno = 16;

for($x = 0; $x <= 3; $x++)
{
    $lower_bracket_results[0][$x][2] = $matchno++;
}

编辑问题后编辑

因为在你的代码中你有一个双重

$lower_bracket_results[] = $inner;
$lower_bracket_results[] = $inner;

我假设您需要构建此阵列的2倍但仍然会增加$matchno。您的问题是您将相同的$inner数组分配给数组$lower_bracket_results中的2个条目。您可以在函数中包装用于创建内部数组的逻辑,并将其调用2次。 $matchnoInBlockpassed by reference,因此在函数内部可以增加主上下文传递的相同变量的值。

这里看起来如何,而不会改变你编写它的方式:

<?php

function createInnerArray($curr, $offset, &$matchnoInBlock) {
    $inner = array();
    for ($i2 = 0; $i2 < $curr; $i2++)
    {
        $matchnoInBlock++;
        $matchno = $matchnoInBlock + $offset;

        $inner[] = array(0, 0, $matchno);
    }
    return $inner;
}

$max = 16;
$tournament_size = $max / 2;
$rounds = log($tournament_size) / log(2);

$curr = $tournament_size / 2;

$offset = 16;
$matchnoInBlock = -1;

for ($i = 0; $i < $rounds; $i++) 
{
    $lower_bracket_results[] = createInnerArray($curr, $offset, $matchnoInBlock);;
    $lower_bracket_results[] = createInnerArray($curr, $offset, $matchnoInBlock);;

    $curr /= 2;
}

echo '<pre>';
print_r($lower_bracket_results);
echo '</pre>';