【问题标题】:Printing a multi dimensional array in table using For loop [closed]使用 For 循环在表中打印多维数组 [关闭]
【发布时间】:2013-04-15 00:41:57
【问题描述】:

我想只使用 For 循环在表中打印一个多维数组。 这是$myArray

$myArray =    Array(
[0] => Array
    (
        [0] => 598
        [1] => Introducing abc
        [2] => 
    )
[1] => Array
    (
        [0] => 596
        [1] => Big Things Happening at abc
        [2] => 
    )
[2] => Array
    (
        [0] => 595
        [1] => Should I send abc?
        [2] => 
    )
[3] => Array
    (
        [0] => 586
        [1] => Things you need to know about abc :P
       [2] => 
    )  

);

将新数组更新为var_dump($myArray );

【问题讨论】:

  • 查看所有在右侧显示为“相关”的问题,例如this one。你真的确定那里没有任何东西可以做你想做的事吗?
  • 不,他们帮不了我。

标签: php for-loop multidimensional-array


【解决方案1】:

有很多不同的方法来解决这个问题,所以为什么不从中找点乐子呢。

如果您必须使用 for 循环

不知道你为什么会这样做,除非是为了学校作业:

for($i=0;$i<count($data);$i++) {
  echo('<tr>');
  echo('<td>' . $data[$i][0] . '</td>');
  echo('<td>' . $data[$i][1] . '</td>');
  echo('<td>' . $data[$i][2] . '</td>');
  echo('</tr>');
}

但是直接访问 ID 有点愚蠢,让我们在行中使用另一个 for 循环:

for($i=0;$i<count($data);$i++) {
  echo('<tr>');
  for($j=0;$j<count($data[$i]);$j++) {
    echo('<td>' . $data[$i][$j] . '</td>');
  } 
  echo('</tr>');
}

用同样无聊的 foreach 循环替换它:

<table>
<?php foreach($items as $row) {
  echo('<tr>');
  foreach($row as $cell) {
    echo('<td>' . $cell . '</td>');
  }
  echo('</tr>');
} ?>
</table>

为什么不内爆数组:

<table>
<?php foreach($items as $row) {
  echo('<tr>');
  echo('<td>');
  echo(implode('</td><td>', $row);
  echo('</td>');
  echo('</tr>');
} ?>
</table>

混合起来,拧紧前叉,然后去散步;并一路爆破东西:

<?php
function print_row(&$item) {
  echo('<tr>');
  echo('<td>');
  echo(implode('</td><td>', $item);
  echo('</td>');
  echo('</tr>');
}
?>

<table>
  <?php array_walk($data, 'print_row');?>
</table>

双走……天哪

是的,现在看起来有点傻,但是当你扩大表格并且事情变得更复杂时,事情会更好地分解和模块化:

<?php
function print_row(&$item) {
  echo('<tr>');
  array_walk($item, 'print_cell');
  echo('</tr>');
}

function print_cell(&$item) {
  echo('<td>');
  echo($item);
  echo('</td>');
}
?>

<table>
  <?php array_walk($data, 'print_row');?>
</table>

【讨论】:

  • @duellsy 有时了解基础知识非常重要(而且很好!)!谢谢;我完全忘记了这一点。 ;-)
【解决方案2】:

使用这个,其实是两个嵌套的for循环:

print('<table>');
for($i = 0; $i < count($array); $i++) {
    print('<tr>');
    for($ii = 0; $ii < count($array[$i]); $ii++) {
        print("<td>{$array[$i][$ii]}</td>");
    }
    print('</tr>');
}
print('</table>');

【讨论】:

  • 这并没有按照他的意愿创建表。
  • @Zim84 感谢您的评论。你是对的,操作员在问题的标题中提到了它。检查我的更新。现在它确实输出了一个 html 表格
  • 使用了你的方法,但表格显示为空,像这样`
    `
  • 那么你的数组有问题。我已经三次检查了代码。你能发一个最近的var_dump($array);吗?
  • 嗨 hek2mgl,我使用 var_dump() 更新数组代码,请看这个,发生了什么,
【解决方案3】:

这样做

echo "<table>";
for($i=0;$i<count($your_array);$i++) {
     echo "<tr><td>".$your_array[$i][0]."</td>";
     echo "<td>".$your_array[$i][1]."</td>";
     echo "<td>".$your_array[$i][2]."</td></tr>";
}
echo "</table>";

【讨论】:

  • 嗨,Yogesh,我使用了你的方法,但没有显示数据并将表格显示为&lt;table&gt;&lt;tr&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  • @ShahzadThathal 那么你想要这张桌子怎么样?您可以根据需要编辑此代码。做起来很简单。
【解决方案4】:
echo '<table>';
for($i=0;$i<count($array);$i++) {
 echo '<tr><td>'.$array[$i][0].'</td>';
 echo '<tr><td>'.$array[$i][1].'</td>';
 echo '<tr><td>'.$array[$i][2].'</td></tr>';
}
echo '</table>';

【讨论】:

  • 嗨,Sudo Reboot,我使用了你的方法,但没有显示数据并将表显示为空&lt;table&gt;&lt;tr&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;td&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
【解决方案5】:

这样做

$arr as your array 

然后

echo "<table>";
for($i = 0; $i<count($arr); $i++)
{


    echo '<tr><td>'.$arr[$i][0].'</td>';
    echo '<tr><td>'.$arr[$i][1].'</td>';
    echo '<tr><td>'.$arr[$i][2].'</td></tr>';
}
echo "</table>";

【讨论】:

  • 这并没有按照他的意愿创建表。
  • @Zim84 抱歉,我没有正确阅读问题。这是我的更新答案
猜你喜欢
  • 2013-08-02
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 2014-02-02
  • 2013-02-20
相关资源
最近更新 更多