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!

2
  • 3
    What have you tried Milo? Show us your code, so we can help you on your way Commented Feb 11, 2019 at 22:52
  • 1
    you are getting a json, just use json_decode(..put everything here in quotes, true); Commented Feb 11, 2019 at 23:15

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

You shouldn't use shortcode for PHP opening tags. <?= php.net/manual/en/language.basic-syntax.phptags.php - see changelog
@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.
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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.