1

I am using 3 php arrays to create a table, these three arrays are used to generate table headers.

$array1 = array('A','B');
$array2 = array('S','C');
$array3 = array('A1','B1','C1','D1');

Generate a table with these array values are headers.

I want to create a table like in image using these three array.

enter image description here

2
  • you hope have a html table or a php array 2d? Commented Dec 14, 2016 at 13:28
  • Are the arrays always this size? Commented Dec 14, 2016 at 13:41

1 Answer 1

1

The following code is used to dynamically generate the table that you have shown. You can change the values of $array1, $array2 or $array3 respectively and table is generate dynamically.

<?php

$array1 = array('A','B','C');
$array2 = array('S','C','M');
$array3 = array('A1','B1','C1','D1','E1','F1');
/* Get the length of all arrays */
$lengthArray1 = count($array1);
$lengthArray2 = count($array2);
$lengthArray3 = count($array3);
?>

<table border="1" width="50%">
    <thead>
        <!-- Print the header -->
        <tr>
            <!-- For first empty space -->
            <td></td>
            <?php 
            /* Based on the length of the array display the headers */
            for($i = 0; $i < $lengthArray1; $i++){ ?>
                <!-- Align center and add the colspan wrt to $array length -->
                <td colspan="<?php echo $lengthArray2; ?>" align="center">
                    <?php echo $array1[$i]; ?>
                </td>
            <?php 
            } ?>
        </tr>
        <tr>
            <!-- For first empty space -->
            <td></td>
            <?php 
            /* Loop through all the $array1 so that that many $array2 values will be assigned to $array1 header */
            for($i = 0; $i < $lengthArray1; $i++){ 
                for($j = 0; $j < $lengthArray2; $j++){
            ?>
                <td  align="center">
                    <?php echo $array2[$j]; ?>
                </td>
            <?php 
                }
            } ?>
        </tr>
    </thead>
    <tbody>
        <!-- Loop through $array3 -->
        <?php 
        for($i = 0; $i < $lengthArray3; $i++){ ?>
            <tr>
                <!-- Print $array3 value in first td  -->
                <td><?php echo $array3[$i]; ?></td>
                <?php
                /* To get the remaining td's I have multiplied $array1 X $array2 */
                for($j = 0; $j < ($lengthArray1 * $lengthArray2); $j++){
                ?>
                <td></td>
                <?php
                }
                ?>
            </tr>
        <?php 
        } ?>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

<table border='2px solid black' bgcolor='lightgray' width="50%" style='border-collapse: collapse'> add that to your table tag. looks better :)
@Blueblazer172 Thanks for your inputs. Ya even I had thought of the same. I left it so that no one must raise question on inline css is a bad approach. So I thought let the user handle the css the way he need. I added with = "50%" just to show how the table looks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.