TransWikia.com

Acessar valores JSON via PHP

Stack Overflow em Português Asked by Cristiano Facirolli on November 5, 2021

Senhores,

Estou tentando acessar os valores que estão dentro da casa "values", recuperando o value e o odd porém sem sucesso, estou criando assim:

$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;
    }
  }
}

meu 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"
                                              }
                                        ]
                                     }....

Onde posso estar errando?

Obrigado.

One Answer

Ao nomear a variável de $item->bookmakers as $bets você se confundiu - $bets não é o campo bets do se json, é um item da lista bookmakers. Como o Valdeir comentou, faltou o for em bets, dessa maneira:

$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;
        }
    }
  }
}

Answered by Fred on November 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP