user2672288
user2672288

Reputation:

iterate over array in php

Ok, so I've read loads of articles and I think I'm at risk of duplicating, but cant work this one out.

I have an array that is being returned by a PHP function, I've called it getLeague();

The structure of the array is:

body[0]->position

body[0]->teamname

body[0]->points

and obviously the results increment from 0 -16 as that's the amount of teams in my league.

I'm trying to tabulate the array by calling getLeague() and iterating over the returned array to print into a table.

I'm trying at the minute to work out the basic for each loop, after that I'll shoehorn it into a table. Can you help me with the foreach? I've tried:

<table class="table table-striped">

    <thead>
        <tr>
            <th>Position</th>
            <th>Team</th>
            <th>Points</th>
        </tr>
    </thead>

    <tbody> 

        <?php
        $rows = getLeague();
        foreach ($rows as $row):
            ?>
            <tr>
                <td><?= $row->body->position; ?></td>
                <td><?= $row->body->teamname; ?></td>
                <td><?= $row->body->points; ?></td>
            </tr>
        <? endforeach ?>

    </tbody>

</table>

Any help appreciated.

Upvotes: 0

Views: 160

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

Without seeing more on that data structure, I can't say for certain, but I think you want:

foreach ($rows->body as $row):

And:

$row->position

Upvotes: 3

Related Questions