简体   繁体   中英

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?

<?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>'; )
  3. It is easier to end your 'if elseif else' with brackets.

The reason you get the undefined offset warning is because your are referencing an array key that does not exist.

Your logic never stores any data to the $matches array.

It looks like you should update the statements in your if block to include $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. But, there are still more problems that will cause undefined offset warnings.

In your for loops, you are looping a static number of times. However, you don't don't take into account when the size of the $matches array had a different number of elements.

Instead of looping from 0 to 12 , you should loop through the exact number of elements. So from 0 to sizeof($matches) :

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

The same goes for your inner for loop. Since your array in each row of $matches can have a different number of elements, loop through the 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. It exists for this very purpose. Your code can be simplified a bit by using the 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. 2) you only need to print 1 column (td) at a time since you are looping over them.

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. 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>";
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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