How to filter JSON data using PHP?

Asked

Viewed 414 times

2

I need to pull the leaderboard of football: position, coat of arms, team name, points, games, wins, goal balance. I’m pulling from an API, see the PHP code I tried:

<?php
header("Content-Type: application/json");
$jsonData = file_get_contents("http://json.gazetaesportiva.com/brasileiro-serie-a-2019.json");
$arrData = json_decode($jsonData); // Transforma o seu JSON
// print_r($arrData)

echo "<pre>";
print_r($arrData);
echo "</pre>";

print_r ($arrData->grupos);

foreach($obj2 as $key => $value){

   $tier = $value[0]->nome;
   $division = $value[0]->entries[0]->division;
   
}
?>

This is part of the result:

[17] => stdClass Object
                        (
                            [equipe] => stdClass Object
                                (
                                    [id] => 13
                                    [nome] => Chapecoense
                                    [abreviacao] => CHA
                                    [pais] => Brasil
                                    [imagem] => stdClass Object
                                        (
                                            [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/chapecoense.png
                                            [thumb] => stdClass Object
                                                (
                                                    [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/thumb_chapecoense.png
                                                )

                                        )

                                    [imagem_foi_obtida_via_url] => 0
                                    [slug] => chapecoense
                                    [pagina_site] => chapecoense
                                )

                            [pontos] => 9
                            [jogos] => 12
                            [vitorias] => 2
                            [empates] => 3
                            [derrotas] => 7
                            [gols_pro] => 11
                            [gols_contra] => 20
                            [saldo_gols] => -9
                            [aproveitamento] => 25
                            [penalidades] => 0
                        )

                    [18] => stdClass Object
                        (
                            [equipe] => stdClass Object
                                (
                                    [id] => 1510
                                    [nome] => CSA
                                    [abreviacao] => CSA
                                    [pais] => Brasil
                                    [imagem] => stdClass Object
                                        (
                                            [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/csa.png
                                            [thumb] => stdClass Object
                                                (
                                                    [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/thumb_csa.png
                                                )

                                        )

                                    [imagem_foi_obtida_via_url] => 0
                                    [slug] => csa
                                    [pagina_site] => csa
                                )

                            [pontos] => 7
                            [jogos] => 12
                            [vitorias] => 1
                            [empates] => 4
                            [derrotas] => 7
                            [gols_pro] => 3
                            [gols_contra] => 20
                            [saldo_gols] => -17
                            [aproveitamento] => 19
                            [penalidades] => 0
                        )

                    [19] => stdClass Object
                        (
                            [equipe] => stdClass Object
                                (
                                    [id] => 7
                                    [nome] => Avaí
                                    [abreviacao] => AVA
                                    [pais] => Brasil
                                    [imagem] => stdClass Object
                                        (
                                            [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/avai.png
                                            [thumb] => stdClass Object
                                                (
                                                    [url] => https://img-apifutebol.gazetaesportiva.com/uploads/equipe/imagem/thumb_avai.png
                                                )

                                        )

                                    [imagem_foi_obtida_via_url] => 0
                                    [slug] => avai
                                    [pagina_site] => avai
                                )

                            [pontos] => 5
                            [jogos] => 12
                            [vitorias] => 0
                            [empates] => 5
                            [derrotas] => 7
                            [gols_pro] => 5
                            [gols_contra] => 16
                            [saldo_gols] => -11
                            [aproveitamento] => 13
                            [penalidades] => 0
                        )

                )

        )

)
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: obj2 in C:\wamp\www\esporte\index.php on line <i>13</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0320</td><td bgcolor='#eeeeec' align='right'>387000</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\esporte\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: Invalid argument supplied for foreach() in C:\wamp\www\esporte\index.php on line <i>13</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0320</td><td bgcolor='#eeeeec' align='right'>387000</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\esporte\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
</table></font>

I’m having trouble leaving only the data, and changing their position and formatting. Thanks in advance.

  • what are you specifically trying to generate??? would only be the leaderboard??

  • @Oliveira Yes, only the classification table.

1 answer

7


In your scenario when using $arrData = json_decode($jsonData); is converted the json into an object and you tried to go through that object in a wrong way, note that the data is in attributes of that object, and some attributes may be objects or arrays. To get the classification data it is necessary to access the attribute groups that is an array, and at position 0, which is an object, vc takes the classification attribute which is an array of objects.

Use the var_dump() to display the data.

$url = "http://json.gazetaesportiva.com/brasileiro-serie-a-2019.json";
$data = file_get_contents($url);
$arrData = json_decode($data);
$title = utf8_decode($arrData->nome);
$ano = utf8_decode($arrData->ano);
$arrClassificacao = $arrData->grupos[0]->classificacao;

var_dump($arrData->grupos[0]->classificacao);

With regard to classification, an example would be:

<?php

$url = "http://json.gazetaesportiva.com/brasileiro-serie-a-2019.json";
$data = file_get_contents($url);
$arrData = json_decode($data);

$title = utf8_decode($arrData->nome);
$ano = utf8_decode($arrData->ano);
$arrClassificacao = $arrData->grupos[0]->classificacao;

?>
<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

        <title><?=$title?></title>
    </head>
    <body>
        <h1><?=$title?></h1>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">Classificação</th>
                        <th scope="col">P</th>
                        <th scope="col">J</th>
                        <th scope="col">V</th>
                        <th scope="col">E</th>
                        <th scope="col">D</th>
                        <th scope="col">GP</th>
                        <th scope="col">GC</th>
                        <th scope="col">SG</th>
                        <th scope="col">%</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                foreach ($arrClassificacao as $key => $equipes) { ?> 
                    <tr>
                        <th scope="row"><?php echo ++$key;?></th>
                        <td>
                            <img src="<?php echo $equipes->equipe->imagem->thumb->url;?>" alt="<?php utf8_decode($equipes->equipe->nome);?>">&nbsp
                            <?php echo utf8_decode($equipes->equipe->nome);?>
                        </td>
                        <td><?php echo $equipes->pontos;?></td>
                        <td><?php echo $equipes->jogos;?></td>
                        <td><?php echo $equipes->vitorias;?></td>
                        <td><?php echo $equipes->empates;?></td>
                        <td><?php echo $equipes->derrotas;?></td>
                        <td><?php echo $equipes->gols_pro;?></td>
                        <td><?php echo $equipes->gols_contra;?></td>
                        <td><?php echo $equipes->saldo_gols;?></td>
                        <td><?php echo $equipes->aproveitamento;?></td>
                    </tr>
            <?php } ?>
                </tbody>
            </table>
       </div>
    </body>
</html>
  • 1

    That’s right! Thank you very much! It helped me a lot!

Browser other questions tagged

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