Skip to content
Advertisement

How to print 2D arrays in a table with key name(just one time) and all it’s values php?

Array
(

 [1JANATAMF] = Array
     (
         [0] = 4.10
         [1] = 5.10
         [2] = 6.10
         [3] = 7.10
         [4] = 8.10
         [5] = 5
         [6] = 0.02
         [7] = 5413
     )

 [1STPRIMFMF] = Array
     (
         [0] = 12.90
         [1] = 1.90
         [2] = 16.90
         [3] = 11.90
         [4] = 14.90
         [5] = 24
         [6] = 04
         [7] = 10
     )

I have this kind of arrays. Now I want to print then in a table with the key name of the main arrays (once) on the table row with its all values. Table I Want is like:

1JANATAMF    4.10   5.10  6.10   7.10   8.10   5   0.02  5413
1STPRIMFMF   12.90  1.90  16.90  11.90  14.90  24  04    10

My Code is this which prints all keys together and some values at the end.

foreach ($c as $keys =>$values)
         echo $keys . " ";`enter code here`
     foreach ($values as $key=>$data)
         echo " " . $data . "<br>";

Advertisement

Answer

You have to use simple foreach():

foreach($array as $key=>$value){?>
<tr>
    <td><?php echo $key;?></td>
    <?php foreach($value as $val){?>
        <td><?php echo $val;?></td>
    <?php } ?>
</tr>
<?php }?>

Sample output: https://3v4l.org/gFhO7

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement