John Davidson

How to loop through given array using php and display in table?

0 comments
Message:


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


My Answer:
Here is a sample code to loop through the given array and display it in a table format:

php
// Given array
$mainArray = array(
array("Array1 first item", "Array1 second item", "Array1 third item", "Array1 fourth item", "Array1 fifth item", "Array1 sixth item"),
array("Array2 first item", "Array2 second item", "Array2 third item", "Array2 fourth item", "Array2 fifth item", "Array2 sixth item"),
array("Array3 first item", "Array3 second item", "Array3 third item", "Array3 fourth item", "Array3 fifth item", "Array3 sixth item"),
array("Array4 first item", "Array4 second item", "Array4 third item", "Array4 fourth item", "Array4 fifth item", "Array4 sixth item"),
array("Array5 first item", "Array5 second item", "Array5 third item", "Array5 fourth item", "Array5 fifth item", "Array5 sixth item"),
array("Array6 first item", "Array6 second item", "Array6 third item", "Array6 fourth item", "Array6 fifth item", "Array6 sixth item")
);

// Display table
echo "";
for ($i = 0; $i < 6; $i++) {
echo "";
for ($j = 0; $j < 6; $j++) {
echo "";
}
echo "";
}
echo "
" . $mainArray[$j][$i] . "
";
?>


This code will loop through the given array and display the items in a table format as per your requirement. You can adjust the number of items in each array and the number of arrays in the main array as needed.

Rate this post

3 of 5 based on 5107 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us