1

I have an array like this:

Array (
    [0] => "name" => "John","id" => "1"
    [1] => "name" => "Mary","id" => "2"
    [2] => "name" => "Sean","id" => "3"
)

How can I make a table like this:

1 | John
2 | Mary
3 | Sean

Thank you!

Milo Greene
  • 56
  • 1
  • 6

2 Answers2

3

Loop through the array and output all values as you want it formatted.

foreach($arr as $item){
    echo "$arr[id] | $arr[name]";
}

Or if its a HTML table you want

<table>
    <?php foreach($arr as $item): ?>
        <tr>
            <td><?=  $arr['id'] ?> </td><td> <?= $arr['name']; ?></td>
        </tr>
    <?php endforeach; ?>
</table>
olayemii
  • 590
  • 1
  • 3
  • 16
  • You shouldn't use shortcode for PHP opening tags. `= ` http://php.net/manual/en/language.basic-syntax.phptags.php - see changelog – Second2None Feb 12 '19 at 00:01
  • 1
    @Second2None `=` isn't a short open tag like `` it's a shorthand for `echo`, and it's always available regardless of the `short_open_tag` ini setting in any modern PHP version. – Don't Panic Feb 12 '19 at 01:13
  • Thanks, my mistake. Learn every day :) – Second2None Feb 12 '19 at 01:16
1

Asuming that the name of your arreglo is $users, you can loop through each element and output the data of that element in a row of the table:

<table>
    <thead>
        <th>ID</th>
        <th>Name</th>
    <thead>
    <tbody>
        <?php
        foreach ($users as $user) {
            echo '<tr>';
            echo '<td>'.$user['id'].'</td>';
            echo '<td>'.$user['name'].'</td>';
            echo '</tr>';
        }
        ?>
    </tbody>
</table>
Omari Celestine
  • 1,405
  • 1
  • 11
  • 21