skip to Main Content

I have this array:

$data
stdClass Object(
[data] =>
Array(
[0] =>
stdClass Object(
[home_team] => Yantra Gabrovo
[away_team] => Septemvri Sofia
.....
[odds] =>
stdClass Object(
[1] => 3.17
[X] => 3.24
[2] => 2.2
[1X] => 1.6
[X2] => 1.31
[12] => 1.29
)
)

I want to insert data inside a simple html table so i create this:

$arr = json_decode($response, true);

echo '<table border=1>
<tr>
<th>ID</th>
<th>SQUADRA CASA</th>
<th>......</th>
<th>QUOTA 1</th>
<th>QUOTA X</th>
<th>QUOTA 2</th>
<th>QUOTA 1X</th>
<th>QUOTA X2</th>
<th>QUOTA 12</th>
</tr>';

foreach ($arr["data"] as list ("id" => $id,                                                     "home_team" => $home_team,                                                  "away_team" => $away_team,                                                                                                  "start_date" => $start_date,                                                        "last_update_at" => $last_update_at,                                                        "1" => $quota1,                                                     "x" => $quotax,                                                     "2" => $quota2,                                                     "1x" => $quota1x,                                                       "x2" => $quotax2,                                                       "12" => $quota12 ))                              
{
echo "<tr>
<td>{$id}</td>
....
<td>{$quota1}</td>
<td>{$quotax}</td>
<td>{$quota2}</td>
<td>{$quota1x}</td>
<td>{$quotax2}</td>
<td>{$quota12}</td>
</tr>";

}
echo '</table>';

The problem is the array 0 contains another list called ODDS and if i try to call the data, is not showed that is nested and not show result 1, 2, x etc. Hope you can help me.

2

Answers


  1. <?php
    
    // Decode the JSON response
    $arr = json_decode($response, true);
    
    // Create an HTML table
    echo '<table border="1">';
    echo '<tr>';
    echo '<th>ID</th>';
    echo '<th>Home Team</th>';
    echo '<th>Away Team</th>';
    echo '<th>...</th>';
    echo '<th>Quota 1</th>';
    echo '<th>Quota X</th>';
    echo '<th>Quota 2</th>';
    echo '<th>Quota 1X</th>';
    echo '<th>Quota X2</th>';
    echo '<th>Quota 12</th>';
    echo '</tr>';
    
    // Iterate over the matches
    foreach ($arr['data'] as $match) {
      // Get the quota values
      $quota1 = $match['odds']['1'];
      $quotax = $match['odds']['X'];
      $quota2 = $match['odds']['2'];
      $quota1x = $match['odds']['1x'];
      $quotax2 = $match['odds']['x2'];
      $quota12 = $match['odds']['12'];
    
      // Add a new row to the table
      echo '<tr>';
      echo '<td>' . $match['id'] . '</td>';
      echo '<td>' . $match['home_team'] . '</td>';
      echo '<td>' . $match['away_team'] . '</td>';
      echo '<td>...</td>';
      echo '<td>' . $quota1 . '</td>';
      echo '<td>' . $quotax . '</td>';
      echo '<td>' . $quota2 . '</td>';
      echo '<td>' . $quota1x . '</td>';
      echo '<td>' . $quotax2 . '</td>';
      echo '<td>' . $quota12 . '</td>';
      echo '</tr>';
    }
    
    echo '</table>';
    
    ?>
    
    Login or Signup to reply.
  2. You need to do something like this:

    <?php
    
    $arr = json_decode($response, true);
    
    echo '<table border=1>
    <tr>
    <th>ID</th>
    <th>SQUADRA CASA</th>
    <th>......</th>
    <th>QUOTA 1</th>
    <th>QUOTA X</th>
    <th>QUOTA 2</th>
    <th>QUOTA 1X</th>
    <th>QUOTA X2</th>
    <th>QUOTA 12</th>
    </tr>';
    
    foreach ($arr["data"] as $row)                              
    {
    echo "<tr>
    <td>{$row['id']}</td>
    <td>{$row['home_team']}</td>
    <td>....</td>
    <td>{$row['odds'][1]}</td>
    <td>{$row['odds']['X']}</td>
    <td>{$row['odds'][2]}</td>
    <td>{$row['odds']['1X']}</td>
    <td>{$row['odds']['X2']}</td>
    <td>{$row['odds'][12]}</td>
    </tr>";
    }
    
    echo '</table>';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search