How to print an array with indexes and values in PHP?

Asked

Viewed 3,049 times

3

I have the following matrix in PHP:

$matriz['HGF']['Christus'] = 7;
$matriz['HGF']['Unifor'] = 6;
$matriz['HIAS']['Christus'] = 5;
$matriz['HIAS']['Unifor'] = 4;

I would like to print the matrix in html, displaying indexes and values, like this:

<table>
   <tbody>
       <tr>
           <td> </td>
           <td> Christus </td>
           <td> Unifor </td>
       </tr>
       <tr>
           <td> HGF </td>
           <td align="center"> 7 </td>
           <td align="center"> 6 </td>
       </tr>
       <tr>
           <td> HIAS </td>
           <td align="center"> 5 </td>
           <td align="center"> 4 </td>
       </tr>
   </tbody>
</table>

I was able to do this in a one-dimensional array using the foreach, but I’m not getting it in this matrix.

  • But you want the entire table to be dynamic, done by PHP or just where the result is ?

  • The table is dynamic, formed by data taken from the database (including indexes are dynamic).

2 answers

2


The first foreach will go through the matrix and pick up the indices to create the top of the table and it has a break so that it only runs once, depending on how it is generated this matrix can get disorganized but if all have the same indeces in the same order will be all ok, the second foreach will take the values and fill the table

The code would be as follows :

<?php 
    $matriz = array(
            "HGF" => array(
                    "Christus" =>7,
                    "Unifor" =>6,
            ),
            "HIAS"=>array(
                    "Christus" =>5,
                    "Unifor" =>4,
            )
    );
?>
<body>
    <table>
        <tbody>
            <tr>
                <td> </td>
                <?php 
                    foreach ($matriz as $key => $value) {
                        foreach ($value as $title => $result) {
                            $head .= "<td> " . $title . " </td>";
                        }
                        echo $head;
                        break;
                    }
                    foreach ($matriz as $key => $value) {
                        $body = "<tr>";
                        $body .= "<td> " . $key . " </td>";
                        foreach ($value as $title => $result) {
                            $head .= "<td> " . $title . " </td>";
                            $body .= "<td align='center'>" . $result . "</td>";
                        }
                        $body .= "</tr>";
                        echo $body;
                    }
                ?>
        </tbody>
    </table>
</body>
  • I need td’s to be dynamic as well. Christus and Unifor indexes are just examples. They can change according to what is in the bank.

  • @Camilacolares and how q is generated this matrix , because the answer may depend on how it comes but , I gave an edited for a possible solution, if it suits you or get a doubt just give a touch

  • Perfect, @Viniciusshiguemori!! Thank you so much!!!

2

It would be like, where you typed the values, you will print the matrix:

 <td><?php echo $matriz['HGF']['Cristus'] ?></td>
 <td><?php echo $matriz['HGF']['Unifor'] ?></td>

 <td><?php echo $matriz['HIAS']['Cristus'] ?></td>
 <td><?php echo $matriz['HIAS']['Unifor'] ?></td>

First, make sure you are passing the correct matrix to the page

Browser other questions tagged

You are not signed in. Login or sign up in order to post.