简体   繁体   English

在 for 循环中更改增量值

[英]Changing increment value in for loop

I am trying to set the correct value for $matchno.我正在尝试为 $matchno 设置正确的值。 It should increment by 1 only.它应该只增加 1。 Below is the code and example of the output array and I changed the array manually to show how I would like the array to be.下面是输出数组的代码和示例,我手动更改了数组以显示我希望数组的样子。 The concern is only with $matchno只关心 $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>';

Output of array:-数组的输出:-

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
                )

        )

)

I would like the array to be like this: (only difference is with $matchno)我希望数组是这样的:(唯一的区别是 $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
                )

        )

)

Any help on this thank you对此有任何帮助谢谢

If I got your question correctly, you just need to increment $matchno after assigning it to the array.如果我正确回答了您的问题,您只需要在将$matchno分配给数组后增加它。

$matchno = 16;

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

or in a shorter fashion或以更短的方式

$matchno = 16;

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

Edit after question edit问题编辑后编辑

Since in your code you have a double因为在你的代码中你有一个双

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

I assume you need to build 2 times this array but still incrementing the $matchno .我假设您需要构建这个数组的 2 倍,但仍然增加$matchno Your issue here was that you were assigning the same $inner array to 2 entries in the array $lower_bracket_results .您的问题是您将相同的$inner数组分配给数组$lower_bracket_results中的 2 个条目。 You can wrap the logic for creating the inner array in a function and call it 2 times.您可以将用于创建内部数组的逻辑包装在一个函数中并调用它 2 次。 $matchnoInBlock is passed by reference , so that inside the function you can increase the value of the same variable passed by the main context. $matchnoInBlock通过引用传递的,因此在函数内部,您可以增加主上下文传递的相同变量的值。

Here how it would look like, without changing much of how you wrote it:这是它的外观,没有改变你写它的方式:

<?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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM