3

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

2
  • 1
    Have a look at all those questions SO shows on the right as "related", for example this one. Are you really sure there is nothing in there that will do what you want? Commented Apr 22, 2013 at 7:31
  • No they couldn't help me. Commented Apr 13, 2016 at 7:36

5 Answers 5

28

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>
Sign up to request clarification or add additional context in comments.

1 Comment

@duellsy Sometimes is very importante (and good!) to get to the basics! Thank you; I was completely forget about this. ;-)
4

Use this, which are actually two nested for loops:

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

5 Comments

this doesn't create a table as he wanted.
@Zim84 Thanks for your comment. You are right, the op mentioned it in the headline of the question. Check my update. Now it does output a html table
Used your method but table show empty like this ` <table><tr><td> </td></tr></table>`
Then there is something wrong with your array. I've triple checked the code. can you post a recent var_dump($array); ?
Hi hek2mgl, I update array code using var_dump(), please see this, what is happen,
3

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

2 Comments

Hi Yogesh, I used your method but not show data and show table as <table><tr><td> </td><td> </td><td> </td></tr></table>
@ShahzadThathal So how do you want the table? You can edit this code as you want your output. It's quit simple to do.
3
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>';

1 Comment

Hi Sudo Reboot, I used your method but not show data and show table empty as <table><tr><td> </td><td> </td><td> </td></tr></table>
2

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

2 Comments

this doesn't create a table as he wanted.
@Zim84 sorry aboout that i did not read question properly. Here is my updated answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.