[英]Inserting data in two dimensional array through for loop
我正在嘗試通過 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);
?>
您的代碼有幾個問題:
echo '</table>';
)您收到undefined offset
警告的原因是您正在引用不存在的數組鍵。
您的邏輯永遠不會將任何數據存儲到$matches
數組。
看起來您應該更新if
塊中的語句以包含$matches[$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);
}
現在, $matches
將被填滿。 但是,還有更多的問題會導致undefined offset
警告。
在您的for
循環中,您循環了靜態次數。 但是,當$matches
數組的大小具有不同數量的元素時,您不會考慮在內。
不是從0
循環到12
,而是應該循環遍歷元素的確切數量。 所以從0
到sizeof($matches)
:
for ($row = 0; $row < sizeof($matches); $row++)
您的內部for
循環也是如此。 由於每行$matches
數組可以具有不同數量的元素,因此循環遍歷sizeof($matches[$row])
:
for ($col = 0; $col < sizeof($matches[$row]); $col++)
由於在這兩種情況下您都在迭代數組中的所有元素,因此我認為現在是提及foreach
循環的好時機。 它就是為了這個目的而存在的。 使用 foreach 可以稍微簡化您的代碼:
foreach ($matches as $row) {
echo "<tr>";
foreach ($row as $col) {
echo "<td>" . $col . "</td>";
}
echo "</tr>";
}
請注意,我替換了您的線路:
echo "<tr> <td>". $matches[$row][$col]. "</td><td>". $matches[$row][$col] . "</td><td>". $matches[$row][$col] . "</td><td>". $matches[$row][$col];
這里有兩個問題:1)多次打印$matches[$row][$col]
將在每一列中打印完全相同的值,這不是您想要做的。 2)您一次只需要打印 1 列 (td),因為您正在循環它們。
因此,您需要將行開始和結束標記移到此循環之外,並且每次迭代僅打印一次列數據。 所以代碼看起來像這樣:
foreach ($matches as $row) {
echo "<tr>";
foreach ($row as $col) {
echo "<td>" . $col . "</td>";
}
echo "</tr>";
}
我有一種感覺,您將對邏輯進行一些調整以獲得所需的確切結果,但現在您知道警告的原因以及如何阻止它們。 使用foreach
還可以稍微清理您的代碼。 所以,希望這是一個好的開始..!
這是新代碼:
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.