php 使用 For 循环在表中打印多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16141590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:36:20  来源:igfitidea点击:

Printing a multi dimensional array in table using For loop

phpfor-loopmultidimensional-array

提问by Muhammad Shahzad

I want to Print a multidimensional array in table using For loop only. This is $myArray

我想仅使用 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] => 
    )  

);

);

update a new array as var_dump($myArray );

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

回答by duellsy

There's a tonne of different approaches to this, so why not have some fun with it.

对此有很多不同的方法,那么为什么不从中获得一些乐趣呢。

If you MUST use a for loop

如果您必须使用 for 循环

No idea why you would, unless it's for a school assignment:

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

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>');
}

But then that's kinda stupid directly accessing the ID's, lets use another for loop in the row:

但是,直接访问 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>');
}

Replace it with an equally as boring foreach loop:

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

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

Why not implode the array:

为什么不内爆数组:

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

Mix it up, screw the foreach, and go for a walk; and implode stuff along the way:

把它混合起来,搞砸 foreach,然后去散散步;并沿途内爆东西:

<?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>

Double walking... OMG

双人行走... OMG

Yeah, it looks a little silly now, but when you grow the table and things get more complex, things are a little better broken out and modularized:

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

<?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>

回答by hek2mgl

Use this, which are actually two nested forloops:

使用这个,它实际上是两个嵌套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>');

回答by Sudo Reboot

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>';

回答by Yogesh Suthar

Do like this

这样做

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

回答by chandresh_cool

Do this

做这个

$arr as your array 

then

然后

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