Read / Search information inside a Json with PHP

0

I'm starting now with the most complex queries within PHP. As I have always played WoW I decided to learn a little about API with PHP using the Blizzard API that gives me some information.

After much searching I was able to pull the information into a table, but I had a question: using the pets search as an example, and if I wanted my list to bring me only the pets with a level higher than 10 (pets- > collected- > stats- > level)

Below is the code for how he is returning a table with all the pets and some information

The full json url is: link

<?php


$type = 'character';
$reino = 'Goldrinn';
$char = 'Lokinhaa';
$language = 'pt_BR';
$fields = 'pets';
$api_key = '3ps9yqk69mhjhy435m8sq4razamkwy25';

$url = "https://us.api.battle.net/wow/$type/$reino/$char?fields=$fields&locale=$language&apikey=$api_key"; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data, true); // decode the JSON feed
$pets = $characters['pets']['collected'];

?>

<table>
    <tbody>
        <tr>
            <th>Pet</th>
            <th>Spell</th>
            <th>Level</th>
            <th>Health</th>
            <th>Power</th>
            <th>Speed</th>
        </tr>
        <?php foreach ($pets as $pet) : ?>
        <tr>
            <td> <?php echo $pet['name']; ?> </td>
            <td> <?php echo $pet['spellId']; ?> </td>
            <td> <?php echo $pet['stats']['level']; ?> </td>
            <td> <?php echo $pet['stats']['health']; ?> </td>
            <td> <?php echo $pet['stats']['power']; ?> </td>
            <td> <?php echo $pet['stats']['speed']; ?> </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

?>
    
asked by anonymous 29.08.2018 / 17:29

1 answer

1

In this way it will display only those with a level 10 or greater in your HTML.

<?php foreach ($pets as $pet) : ?>
<?php if($pet['stats']['level']>=10){ ?>
<tr>
    <td> <?php echo $pet['name']; ?> </td>
    <td> <?php echo $pet['spellId']; ?> </td>
    <td> <?php echo $pet['stats']['level']; ?> </td>
    <td> <?php echo $pet['stats']['health']; ?> </td>
    <td> <?php echo $pet['stats']['power']; ?> </td>
    <td> <?php echo $pet['stats']['speed']; ?> </td>
</tr>
<?php } ?>
<?php endforeach; ?>

This format only works by filtering the data you have already received (full), perhaps in the API documentation, you can find a parameter in which you can pass and receive the data already filtered according to your need. >     

29.08.2018 / 18:34