php中的foreach plus for

php中的foreach plus for,php,for-loop,foreach,Php,For Loop,Foreach,我想提出: aaa | bbb | ccc | ddd etc 1 | 1 | 1 | 1 2 | 2 | 2 | 2 3 | 3 | 3 | 3 etc 对于aaa、bbb等,我使用FOREACH <table><tr> foreach ($data as $d){ echo "<td>" . $d . "</td>"; } </tr> for ($i = 0; $i < 20; $

我想提出:

aaa | bbb | ccc | ddd  etc
1   |  1  |  1  | 1 
2   |  2  |  2  | 2 
3   |  3  |  3  | 3
etc
对于aaa、bbb等,我使用FOREACH

<table><tr>
foreach ($data as $d){
echo "<td>" . $d . "</td>";
}
</tr>

for ($i = 0; $i < 20; $i++){
 echo "<tr><td>" . $i . "</td></tr>";
}

foreach($d数据){
回显“$d”;
}
对于($i=0;$i<20;$i++){
回音“$i”;
}

但是这不好用。如何对来自foreach的所有数据使用loop?

我认为您可以这样生成表:

$columns = array('aaa','bbb','ccc','ddd');
$num_cols = count($columns);
echo "<table>";
echo "<tr>";
foreach($columns as $col)
{
   echo "<td>$col</td>";
}
echo "</tr>";

for($i=1;$i<20;$i++)
{
    echo "<tr>";
    for($j=0;$j<$num_cols;$j++)
    {
      echo "<td>$i</td>";
    }
    echo "</tr>";
 }

echo "</table>";
$columns=array('aaa'、'bbb'、'ccc'、'ddd');
$num_cols=计数($columns);
回声“;
回声“;
foreach($列作为$col)
{
回声“$col”;
}
回声“;

对于($i=1;$i我认为您可以如下生成表:

$columns = array('aaa','bbb','ccc','ddd');
$num_cols = count($columns);
echo "<table>";
echo "<tr>";
foreach($columns as $col)
{
   echo "<td>$col</td>";
}
echo "</tr>";

for($i=1;$i<20;$i++)
{
    echo "<tr>";
    for($j=0;$j<$num_cols;$j++)
    {
      echo "<td>$i</td>";
    }
    echo "</tr>";
 }

echo "</table>";
$columns=array('aaa'、'bbb'、'ccc'、'ddd');
$num_cols=计数($columns);
回声“;
回声“;
foreach($列作为$col)
{
回声“$col”;
}
回声“;

对于($i=1;$i通常取决于
$data
的外观

<?php

$data = array('aaa', 'bbb', 'ccc', 'ddd'); // Assuming that $data is a columns storage
$rows = 10; // $rows = count($data); if you wish to have same number of columns and rows

echo '<table>';
echo '<tr>';
foreach ($data AS $item)
{
    echo '<td>' . $item . '</td>';
}
echo '</tr>';
for ($idx = 0; $idx < $rows; $idx++)
{
    echo '<tr>';
    for ($col = 1, $col_num = count($data); $col <= $col_num; $col++)
    {
        echo '<td>' . $idx . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

?>


p.S.尚未测试代码。

通常取决于
$data
的外观

<?php

$data = array('aaa', 'bbb', 'ccc', 'ddd'); // Assuming that $data is a columns storage
$rows = 10; // $rows = count($data); if you wish to have same number of columns and rows

echo '<table>';
echo '<tr>';
foreach ($data AS $item)
{
    echo '<td>' . $item . '</td>';
}
echo '</tr>';
for ($idx = 0; $idx < $rows; $idx++)
{
    echo '<tr>';
    for ($col = 1, $col_num = count($data); $col <= $col_num; $col++)
    {
        echo '<td>' . $idx . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

?>


p.S.尚未测试代码。

请澄清“此工作不正常”的含义…您是否收到错误消息?如果收到,请将其发布。如果您得到的输出与您期望的结果不符,请发布该输出的一个片段。表行的数据在哪里?它不仅仅是111122223333作为默认前缀,对吗?这只为aaa生成了1,2,3,4等。我希望为所有d生成此循环来自FOREACH的ata-aaa、bbb、cccetc@djot我只想在这个例子中使用从for生成的111请澄清“此工作不正常”的含义…您是否收到错误消息?如果收到,请将其发布。如果您得到的输出与您期望的结果不符,请发布该输出的一个片段。表行的数据在哪里?它不仅仅是111122223333作为默认前缀,对吗?这只为aaa生成了1,2,3,4等。我希望为所有d生成此循环来自FOREACH的ata-aaa、bbb、cccetc@djot我只想在这个例子中使用111生成的FOR@eueka:现在只需生成$jseems您忘记了启动
这是不正确的,数组计数在这里总是=$j+1case@eueka:现在只需生成$jseems您忘记启动
这是不正确的,数组计数是alwa在这种情况下,ys=$j+1