Greetings, I encountered such a task, I recently started working with php, and this task caused me difficulties.

In general, there is a table, it is filled with numbers, if at least in one cell there is a number, then we leave a row. If the row is completely empty, then delete it altogether, that is, do not display this row in the table.

For clarity, the picture:

picture

How can this function be implemented?

  • output from the index array loop foreach? - Kirill Korushkin
  • @KirillKorushkin simply through for - Mr.Flatman
  • one
    show source array - Kirill Korushkin
  • one
    Telepaths fled ... Code where? - Qwertiy

2 answers 2

$cols = 7; $arr = [ ["1","2","3"], [], ]; echo '<table>'; foreach( $arr as $el ) { echo '<tr>'; if($el) { for($i = 0; $i < $cols; $i++) { if($el && $i < count($el)) { echo '<td>' . $el[$i] . '</td>'; }else { echo '<td></td>'; } } } echo '</tr>'; } echo '</table>'; 

example

    Example

     <?php function getMaxArrayCount($array) { $maxCount = 0; foreach ($array as $innerArray) { $arrayCount = count($innerArray); if($arrayCount > $maxCount) { $maxCount = count($innerArray); } } return $maxCount; } ?> <table> <?php $myArray = [ ["one", "two", "three"], ["test"] ]; foreach ($myArray as $innerArray) { $innerCount = count($innerArray); if ($innerCount > 0) { echo "<tr>"; for($j = 0; $j < $innerCount; $j++) { echo "<td>". $innerArray[$j] ."</td>"; } for($k = 0; $k < getMaxArrayCount($myArray) - $innerCount; $k++) { echo "<td></td>"; } echo "</tr>"; } } ?> </table>