Access JSON values via PHP

Asked

Viewed 58 times

1

Sirs,

I am trying to access the values that are inside the house "values", recovering the value and the Odd but without success, I am creating so:

$json = json_decode($response);
foreach ($json->response as $item) {
echo $item->league->name . '<br>';
echo $item->league->country . '<br>';

foreach($item->bookmakers as $bets){
    echo ' - ' . $bets->id . ' - ' . $bets->name . '<br>';
    foreach ($bets->values as $odds) {
        echo $odds->value;
        echo $odds->odd;
    }
  }
}

my json

"response": [ {
            "league": { 
            "id": 562,
            "name": "Reserve League",
            "country": "Belarus",
            "logo": "https://media.api-sports.io/football/leagues/562.png",
            "flag": "https://media.api-sports.io/flags/by.svg",
            "season": 2020
         },
         "fixture": {
                     "id": 430126,
                     "timezone": "UTC",
                     "date": "2020-05-15T10:30:00+00:00",
                     "timestamp": 1589538600
          },
         "update": "2020-05-15T09:49:33+00:00",
        "bookmakers": [
                  {
                       "id": 6,
                       "name": "Bwin",
                       "bets": [
                               {
                                   "id": 1,
                                   "name": "Match Winner",
                                   "values": [
                                              {
                                                  "value": "Home",
                                                  "odd": "1.90"
                                              },
                                              {
                                                  "value": "Draw",
                                                  "odd": "4.10"
                                              },
                                              {
                                                  "value": "Away",
                                                  "odd": "2.95"
                                              }
                                        ]
                                     }....

Where can I be missing?

Thank you.

  • I think it’s worth you posting yours JSON in text form here. Although it is not very big, the way it is you need another user to type it whole to be able to simulate your problem.

  • Edited question with JSON

  • You have to want to use one for in Bets, that’s inside bookmakers

  • In the @Valdeirpsr case it would be like this for($Bets = 0; $Bets < Count($item->bookmakers); $Bets++){ echo $Bets; }

1 answer

1


When naming the variable of $item->bookmakers as $bets you got confused - $Bets is not the Bets field of se json, it is an item in the bookmakers list. As Valdeir commented, missed the for in Bets, that way:

$json = json_decode($response);
foreach ($json->response as $item) {
echo $item->league->name . '<br>';
echo $item->league->country . '<br>';

foreach($item->bookmakers as $betsy){
    echo ' - ' . $betsy->id . ' - ' . $betsy->name . '<br>';
    foreach ($betsy->bets as $odds) {
        foreach($odds->values as $o){
            echo $o->value;
            echo $o->odd;
        }
    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.