使用For循环在表格中打印多维数组 [英] Printing a multi dimensional array in table using For loop

查看:125
本文介绍了使用For循环在表格中打印多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用For循环打印一个多维数组。
这是 $ myArray

  $ myArray = Array 
[0] =>数组

[0] => 598
[1] =>介绍abc
[2] =>

[1] =>数组

[0] => 596
[1] => abc
数组

[0] => 595
[1] =>我应该发送abc?
[2] =>

[3] =>数组

[0] => 586
[ 1] =>你需要知道的关于abc的东西:P
[2] =>

$ b $ (b

);

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

解决方案

这个有很多不同的方法,所以为什么不用它呢。

如果你必须使用for循环



不知道为什么, ($ i = 0; $ i

  
echo('< tr>);
echo('< td>。$ data [$ i] [0]。'< / td>');
echo('< td>。$ data [$ i] [1]。'< / td>');
echo('< td>。$ data [$ i] [2]。'< / td>');
echo('< / tr>);





$ b

但是直接访问这个ID有点笨,让我们用另一个for循环($ i = 0; $ i< count($ data); $ i ++){$ b $($ i $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b echo('< tr>'); ($ j = 0; $ j< count($ data [$ i]); $ j ++){
echo('< td>。$ data [$ i] [$ j ]。'< / td>');
}
echo('< / tr>');

$ / code $ / $ p

用一个无聊的foreach循环代替它:



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



为什么不使用数组implode:

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



把它们混合起来,拧上foreach,去散步,一路上爆料:



 <?php $ ($< tr>); 
echo('< ; / td>< td>',$ item);
echo('< / td>);
echo('< / tr>);
}
?>

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



双重行走... OMG



它现在看起来有点傻,但是当你长大桌子,事情变得更加复杂的时候,事情会变得更好一点,模块化:

 <$ c ($ item){
echo('< tr>);
array_walk($ item, 'print_cell');
echo('< / tr>);


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

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


I want to Print a multidimensional array in table using For loop only. This is $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 );

解决方案

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

If you MUST use a for loop

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:

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:

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

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

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>

这篇关于使用For循环在表格中打印多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆