Drukowanie wielowymiarowej tablicy w tabeli za pomocą pętli For [zamkniętej]

Chcę wydrukować wielowymiarową tablicę w tabeli używając tylko pętli For. To jest $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] => 
    )  

);

Zaktualizuj nową tablicę jako var_dump($myArray );

Author: Muhammad Shahzad, 2013-04-22

5 answers

Istnieje mnóstwo różnych podejść do tego, więc dlaczego nie mieć trochę zabawy z nim.

Jeśli musisz użyć pętli for

Nie wiem dlaczego, chyba że to zadanie szkolne: {]}

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

Ale To jest trochę głupie bezpośrednio dostęp do ID' ów, pozwala użyć innej pętli for w wierszu:

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

Zastąp ją równie nudną pętlą foreach:

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

Dlaczego nie implodować tablicy:

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

Wymieszać, przykręcić foreach, i iść na spacer; i implodować rzeczy po drodze:

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

Podwójne chodzenie... OMG

Tak, wygląda to trochę głupio teraz, ale kiedy rośnie stół i rzeczy stają się bardziej złożone, rzeczy są trochę lepiej rozłożone i modularyzowane: {]}
<?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>
 24
Author: duellsy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-01 21:06:57

Użyj tego, które są w rzeczywistości dwiema zagnieżdżonymi pętlami 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>');
 4
Author: hek2mgl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-22 07:45:04

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>";
 3
Author: Yogesh Suthar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-22 07:28:49
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>';
 3
Author: Sudo Reboot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-22 07:32:22

Zrób to

$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>";
 2
Author: chandresh_cool,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-22 07:40:42