I have array like this in php:
$arr = [
[
"group": 1,
"name": John
],
[
"group": 1,
"name": Luke
],
[
"group": 1,
"name": Peter
],
[
"group": 2,
"name": Pia
]
]
expected output in html table:
GROUP | NAME
------|-----
| John
1 | Luke
| Peter
------|-----
2 | Pia
I attempted to work around the foreach
but i cant figure it out. Below is the last code i tried:
<table>
<tr>
<th>GROUP</th>
<th>NAME</th>
</tr>
<tbody>
<?php $group = ''; ?>
<?php foreach($results AS $result) : ?>
<tr>
<td>
<?php if($group !== $result['group']): ?>
<?= $result['group'] ?>
<?php endif; ?>
</td>
<td>
<ul>
<li><?= $result['name'] ?></li>
</ul>
</td>
</tr>
<?php $group = $result['group']; ?>
<?php endforeach; ?>
</tbody>
</table>
but the result is:
GROUP | NAME
------|-----
1 | John
------|-----
| Luke
------|-----
| Peter
------|-----
2 | Pi
I need to show it as the about expected output.
Please help, thanks