简体   繁体   English

通过for循环在二维数组中插入数据

[英]Inserting data in two dimensional array through for loop

I'm trying to fill a two dimensional array in php through a for loop, the function is about generating a matches schedule, for a certain reason the php page echoes an undefined offset... any help?我正在尝试通过 for 循环在 php 中填充一个二维数组,该函数是关于生成一个匹配计划,出于某种原因,php 页面回显了一个未定义的偏移量......有什么帮助吗?

<?php
function generateMatches($size)
{
    $matches = array();
    $step = 3;
    if ($size % 4 == 0)
    {
        for ($i = 0; $i < ($size - $step); $i++)
        {
            if ($i < $size / 4)
            {
                array($i + 1, $i + $step, $i + (2 * $step), $i + (3 * $step));
            }
            else if ($i >= ($size / 4) && $i < ($size / 2))
                array($i + 1, $i + $step, $i + (2 * $step));
            else
                array($i + 1, $i + $step);
        }
    }
    echo "<table>
            <tr>
              <td> Team # </td>
              <td> Oppenent 1 </td>
              <td> opponent 2 </td>
              <td> opponent 3 </td>
            </tr>
            <tr>";

    for ($row = 0; $row < 13; $row++)
        for($col = 0; $col < 4; $col++)
            echo "<tr> <td>". $matches[$row][$col]. "</td><td>". $matches[$row][$col] . "</td><td>". $matches[$row][$col] . "</td><td>". $matches[$row][$col];
}
generateMatches(12);

?> ?>

There are serveral problems with your code:您的代码有几个问题:

  1. The array matches is not filled.数组匹配未填充。
  2. Your table is not finished ( echo '</table>'; )你的桌子没有完成( echo '</table>';
  3. It is easier to end your 'if elseif else' with brackets.用括号结束“if elseif else”更容易。

The reason you get the undefined offset warning is because your are referencing an array key that does not exist.您收到undefined offset警告的原因是您正在引用不存在的数组键。

Your logic never stores any data to the $matches array.您的逻辑永远不会将任何数据存储到$matches数组。

It looks like you should update the statements in your if block to include $matches[$i] = .看起来您应该更新if块中的语句以包含$matches[$i] = So it would look like this:所以它看起来像这样:

        if ($i < $size / 4) {
            $matches[$i] = array($i + 1, $i + $step, $i + (2 * $step), $i + (3 * $step));

        } else if ($i >= ($size / 4) && $i < ($size / 2)) {
            $matches[$i] = array($i + 1, $i + $step, $i + (2 * $step));

        } else {
            $matches[$i] = array($i + 1, $i + $step);
        }

Now, $matches will get filled.现在, $matches将被填满。 But, there are still more problems that will cause undefined offset warnings.但是,还有更多的问题会导致undefined offset警告。

In your for loops, you are looping a static number of times.在您的for循环中,您循环了静态次数。 However, you don't don't take into account when the size of the $matches array had a different number of elements.但是,当$matches数组的大小具有不同数量的元素时,您不会考虑在内。

Instead of looping from 0 to 12 , you should loop through the exact number of elements.不是从0循环到12 ,而是应该循环遍历元素的确切数量。 So from 0 to sizeof($matches) :所以从0sizeof($matches)

for ($row = 0; $row < sizeof($matches); $row++)

The same goes for your inner for loop.您的内部for循环也是如此。 Since your array in each row of $matches can have a different number of elements, loop through the sizeof($matches[$row]) :由于每行$matches数组可以具有不同数量的元素,因此循环遍历sizeof($matches[$row])

for ($col = 0; $col < sizeof($matches[$row]); $col++)

Since in both of these cases you are iterating over all of the elements in an array, I suppose it's a good time to mention the foreach loop.由于在这两种情况下您都在迭代数组中的所有元素,因此我认为现在是提及foreach循环的好时机。 It exists for this very purpose.它就是为了这个目的而存在的。 Your code can be simplified a bit by using the foreach:使用 foreach 可以稍微简化您的代码:

foreach ($matches as $row) {
    echo "<tr>";

    foreach ($row as $col) {
        echo "<td>" . $col . "</td>";
    }

    echo "</tr>";
}

Notice I replaced your line:请注意,我替换了您的线路:

    echo "<tr> <td>". $matches[$row][$col]. "</td><td>". $matches[$row][$col] . "</td><td>". $matches[$row][$col] . "</td><td>". $matches[$row][$col];

There are 2 problems here: 1) printing $matches[$row][$col] multiple times is going to print the exact same value in every column, which is not what you are trying to do.这里有两个问题:1)多次打印$matches[$row][$col]将在每一列中打印完全相同的值,这不是您想要做的。 2) you only need to print 1 column (td) at a time since you are looping over them. 2)您一次只需要打印 1 列 (td),因为您正在循环它们。

So you need to move the row opening and closing tags outside of this loop and only print the column data once per iteration.因此,您需要将行开始和结束标记移到此循环之外,并且每次迭代仅打印一次列数据。 So the code looks like this:所以代码看起来像这样:

foreach ($matches as $row) {
    echo "<tr>";

    foreach ($row as $col) {
        echo "<td>" . $col . "</td>";
    }

    echo "</tr>";
}

I have a feeling that you're going to do some tweaking to your logic to get the exact results you want, but now you know the reason for the warnings and and how to stop them.我有一种感觉,您将对逻辑进行一些调整以获得所需的确切结果,但现在您知道警告的原因以及如何阻止它们。 Using foreach also cleans up your code a bit.使用foreach还可以稍微清理您的代码。 So, hopefully this is a good start..!所以,希望这是一个好的开始..!

Here's the new code altogether:这是新代码:

function generateMatches($size)
{
    $matches = array();
    $step = 3;

    if ($size % 4 == 0) {
        for ($i = 0; $i < ($size - $step); $i++) {

            if ($i < $size / 4) {
                $matches[$i] = array($i + 1, $i + $step, $i + (2 * $step), $i + (3 * $step));

            } else if ($i >= ($size / 4) && $i < ($size / 2)) {
                $matches[$i] = array($i + 1, $i + $step, $i + (2 * $step));

            } else {
                $matches[$i] = array($i + 1, $i + $step);

            }
        }

    }

    echo "<table><tr><td> Team # </td><td>Opponent 1</td><td>opponent 2</td><td>opponent 3</td></tr>";


    foreach ($matches as $row) {
        echo "<tr>";

        foreach ($row as $col) {
            echo "<td>" . $row[$col] . "</td>";
        }

        echo "</tr>";
    }

}

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

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