0

Could you help me guys how to make an html table from this array. I don't know php much. I have searched the net and find that I must use a foreach loop but the arrays I found have different form.

$a = array(
1 => 1,
2 => 2,
3 => 3,
4 => 4,
);

$c = array(
1 => array(
    'tutył' => 'treść1'
    'kol1' => '11'
    'kol2' => '00'
    'kol3' => '22'
    )
2 => array(
    'tutył' => 'treść2'
    'kol1' => '12'
    'kol2' => '10'
    'kol3' => '23'
    )
2 => array(
    'tutył' => 'treść3'
    'kol1' => '1'
    'kol2' => '2'
    'kol3' => '3'
    )
2 => array(
    'tutył' => 'treść4'
    'kol1' => '1'
    'kol2' => '2'
    'kol3' => '3'
    )
);

Could you help me with this problem ?

jszobody
  • 28,495
  • 6
  • 61
  • 72
user1597594
  • 523
  • 5
  • 9

2 Answers2

1

Try using foreach:

$first = true;
foreach($c as $row) {
    foreach($row as $key => $value) {
        if ($first) {
            //print a cell for the table-header, use $key
            $first = false;
        } else {
            //print regular values, use $value
        }
    }
}

Note that your example array has 2 point to three different values, so you would only get one of them.

Rogue
  • 11,105
  • 5
  • 45
  • 71
1

Something like:

<table>
<?php foreach($c as $key => $data): ?>
    <tr>
        <td><?= $data['tutył']; ?></td>
        <td><?= $data['kol1']; ?></td>
        <td><?= $data['kol2']; ?></td>
        <td><?= $data['kol3']; ?></td>
    </tr>
<?php endforeach; ?>
</table>
keithhatfield
  • 3,273
  • 1
  • 17
  • 24