Programmers learn & share
26 views

Solution:

STEP 1: Though it is not a conventional way however you can try this code

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

 

STEP 2: Using 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>');
}

 

STEP 3: You can replace it with foreach loop

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

 

STEP 4: implode the array

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

 

STEP 5: Mix it up and implode stuff try this 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>

 

STEP 6: 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>');

 

posted May 19 in php by (10,020 points)   | 26 views
2,221 questions
2,697 answers
59 comments
241 users