Home > Software design >  How to loop through given array using php and display in table?
How to loop through given array using php and display in table?

Time:09-12

I have a array which has 6 arrays.

mainArray = array(array1, array2, array3, array4, array5, array6);

Array 1 to Array 6 : Each has 56 items in it.

I want to display the items in table like below:

1 2 3 4 5 6
Array1 first item Array2 first item Array3 first item Array4 first item Array5 first item Array6 first item
Array1 second item Array2 second item Array3 second item Array4 second item Array5 second item Array6 second item
Array1 Third item Array2 Third item Array3 Third item Array4 Third item Array5 Third item Array6 Third item
Array1 Fourth item Array2 Fourth item Array3 Fourth item Array4 Fourth item Array5 Fourth item Array6 Fourth item
Array1 Fifth item Array2 Fifth item Array3 Fifth item Array4 Fifth item Array5 Fifth item Array6 Fifth item
Array1 Sixth item Array2 Sixth item Array3 Sixth item Array4 Sixth item Array5 Sixth item Array6 Sixth item
Array1 Seventh item Array2 Seventh item Array3 Seventh item Array4 Seventh item Array5 Seventh item Array6 Seventh item
Array1 Eighth item Array2 Eighth item Array3 Eighth item Array4 Eighth item Array5 Eighth item Array6 Eighth item
1 2 3 4 5 6
Array1 9th item Array2 9th item Array3 9th item Array4 9th item Array5 9th item Array6 9th item
Array1 10th item Array2 10th item Array3 10th item Array4 10th item Array5 10th item Array6 10th item
Array1 11th item Array2 11th item Array3 11th item Array4 11th item Array5 11th item Array6 11th item
Array1 12th item Array2 12th item Array3 12th item Array4 12th item Array5 12th item Array6 12th item
Array1 13th item Array2 13th item Array3 13th item Array4 13th item Array5 13th item Array6 13th item
Array1 14th item Array2 14th item Array3 14th item Array4 14th item Array5 14th item Array6 14th item
Array1 15th item Array2 15th item Array3 15th item Array4 15th item Array5 15th item Array6 15th item
Array1 16th item Array2 16th item Array3 16th item Array4 16th item Array5 16th item Array6 16th item

And it continues till 56 items.

I know to loop through we need foreach, but how to break array into 8 like above and then loop again.

Any suggestions or guide ?

CodePudding user response:

There are many ways to solve this, but since it is a small table you can do it in a way that is easy to understand. First resort your array with columns into an array with rows. Then it is easy to display those rows as a table. Something like this:

$mainArray = [<here is all your data>];

// first resort
$tableRows = [];
foreach ($mainArray as $columnIndex => $columnArray) {
    foreach ($columnArray as $rowIndex => $item) {
        $tableRows[$rowIndex][$columnIndex] = $item;
    }
}

// then display
echo "<table>";
foreach ($tableRows as $tableRow) {
    echo "<tr>";
    foreach ($tableRow as $tableCell) {
        echo "<td>$tableCell</td>";
    }
    echo "</tr>";
}
echo "</table>";

This will work even if the size of your array varies. Note that things might go wrong if not all the column arrays have the same amount of items in them.

CodePudding user response:

You can use modulo operator with loop counter to print delimiter between each X rows, like this:

$split = 8;

for ($i = 0; $i < count($array[0]); $i  ) {
    // Print row here, loop for all arrays using $i as index 
    
    if (($i   1) % $split === 0) {
        // Print delimiter between tables here
    }
}
  • Related