简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM