Balrajswamy
Balrajswamy

Reputation: 65

How to create a table from three arrays using PHP with foreach loop?

I do not know how to create a table for three arrays using PHP with a foreach loop.

Code as follows:

$a = array ("Rama", "Seetha", "Kannan", "Shiva");
$b = array ("12", "10", "15", "17");
$c = array ("11", "b1", "d2", "10");

Expected output as follows:

Sno Name    Age Id
1   Rama    12  11
2   Seetha  10  b1
3   Kannan  15  d2
4   Shiva   17  10

Upvotes: 1

Views: 40

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

$a = array ("Rama", "Seetha", "Kannan", "Shiva");
$b = array ("12", "10", "15", "17");
$c = array ("11", "b1", "d2", "10");

$count = count($a);

?>
<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Age</th>
            <th>Sno</th>
        </tr>
    </thead>
    <tbody> 
    <?php
        $j = 1;
        for ($i=0; $i < $count ; $i++) 
        { 
            $name = $a[$i];
            $age = $b[$i];
            $id = $c[$i];

            ?>
            <tr>
                <td><?php echo $j ?></td>
                <td><?php echo $name ?></td>
                <td><?php echo $age ?></td>
                <td><?php echo $id ?></td>
            </tr>
            <?php
            $j++;
        }
    ?>
    </tbody>
</table>

Upvotes: 1

Related Questions