0

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.

3
  • 1
    php.net/manual/en/function.json-decode.php Commented Sep 19, 2019 at 8:24
  • But where is your raw data of player names from? Commented Sep 19, 2019 at 8:28
  • Could you edit your question and include an minimal example of the JSON payload, since the external site i.e. freerdarts.com and the link might break over time. Commented Sep 19, 2019 at 8:30

1 Answer 1

1

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

2 Comments

Thanks LucaThemes. I have tried to implement the array, but I must be missing something here. Please see here tehplayground.com/OWRTTm82SCI7lyb1
@KJThaDon it's my fault, sorry. I have updated my answer. You can't echo an array directly, it has to be converted to a string first. implode() function will merge all array elements into one string, putting a comma in between them (or any character you prefer). If there is only one name per team like in your example, no comma will be displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.