KJThaDon
KJThaDon

Reputation: 416

Replacing text from json objects to php

Here's the json I have that is pulling data from a 3rd party page.
http://freerdarts.com/api/tues_standings_api_2019.php

I am formatting the data into a table like this.

<table class="tbl_container" id="standings">
  <thead>
    <tr>
      <th>TEAM</th>
      <th>WIN %</th>
      <th>GAMES</th>
      <th>WON</th>
    </tr>
  </thead>
  <tbody>
    <?php
      foreach($standings as $row):
      ?>
    <tr>
      <td><?=$row['team'];?></td>
      <td><?=$row['win%'];?></td>
      <td><?=$row['games'];?></td>
      <td><?=$row['wins'];?></td>
    </tr>
    <?php endforeach;?>
  </tbody>
</table>

So under the first td 'team' it inserts the 10 teams, but I want to be able to replace the data with player names instead of the 'Team 01' or 'Team 02' etc...For each one, it pulls I need to substitute for a person name.

Upvotes: 0

Views: 52

Answers (1)

Lukasz Formela
Lukasz Formela

Reputation: 447

You probably need to define players like this:

$players = [
   '01' => [
       'name1',
       'name2',
       'name3',
   ],
   '02' => [
       'name1',
       'name2',
       'name3',
   ],
];

Then read the array as follows:

<tbody>
    <?php 
        foreach($standings as $row):
        $team_number = explode(' ', $row['team'])[1]; 
        ?>
        <tr>
           <td><?=implode(',', $players[$team_number]);?></td>
           <td><?=$row['win%'];?></td>
           <td><?=$row['games'];?></td>
           <td><?=$row['wins'];?></td>
        </tr>
    <?php endforeach;?>
</tbody>

Upvotes: 1

Related Questions