Jenndale Merza Jenndale Merza - 2 months ago 15
PHP Question

How to get the key and print it inside an HTML tag?

I am new in PHP and I my problem is I want to pass the keys/value in my table. Is there any other way to tdo this? Thank you!

Here are my codes:

<?php
$array = array(
"Salary of Mr. A is " => "1000$",
"Salary of Mr. B is " => "1200$",
"Salary of Mr. C is " => "1400$");
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table border="1">
<tr>
<td><?= $array[0]; ?></td>
<td><?= $array["Salary of Mr. A is "]; ?></td>
</tr>
<tr>
<td></td>
<td><?= $array["Salary of Mr. B is "]; ?></td>
</tr>
<tr>
<td></td>
<td><?= $array["Salary of Mr. C is "]; ?></td>
</tr>
</table>
</body>
</html>

Answer

Another way is

    <?php
        $array = array(
        "Salary of Mr. A is " => "1000$",
        "Salary of Mr. B is " => "1200$",
        "Salary of Mr. C is " => "1400$");              
    ?>
    <!DOCTYPE html>
    <html>
    <head>
         <title></title>
    </head>
    <body>
    <table border="1">
    <?php foreach ($array as $key => $value) { ?>
        <tr>
            <td><?php echo $key; ?></td>
            <td><?php echo $value; ?></td>
        </tr>
    <?php } ?>
    </table>
    </body>
     </html>