Can't display JSON nested arrays with HTML table and PHP

Viewed 80

I am trying to display the table that shows the latest vaccination data in each country. I managed to pull out the list of countries, but I could not display the nested arrays of the latest data, total vaccinated, and total vaccinations per hundred.

I want to display the array of total_vaccinations and total_vaccinations_per_hundred that contains the latest date value from date.

I am open to using jQuery and Javascript to make this work.

Here is the small section of the JSON data. The original data is here: https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.json

Array
(
    [0] => Array
        (
            [country] => Afghanistan
            [iso_code] => AFG
            [data] => Array
                (
                    [0] => Array
                        (
                            [date] => 2021-02-22
                            [total_vaccinations] => 0
                            [people_vaccinated] => 0
                            [total_vaccinations_per_hundred] => 0
                            [people_vaccinated_per_hundred] => 0
                        )

                    [1] => Array
                        (
                            [date] => 2021-02-23
                            [daily_vaccinations] => 1367
                            [daily_vaccinations_per_million] => 35
                        )

                    [2] => Array
                        (
                            [date] => 2021-02-24
                            [daily_vaccinations] => 1367
                            [daily_vaccinations_per_million] => 35
                        )

                    [3] => Array
                        (
                            [date] => 2021-02-25
                            [daily_vaccinations] => 1367
                            [daily_vaccinations_per_million] => 35
                        )

                    [4] => Array
                        (
                            [date] => 2021-02-26
                            [daily_vaccinations] => 1367
                            [daily_vaccinations_per_million] => 35
                        )

                    [5] => Array
                        (
                            [date] => 2021-02-27
                            [daily_vaccinations] => 1367
                            [daily_vaccinations_per_million] => 35
                        )

                    [6] => Array
                        (
                            [date] => 2021-02-28
                            [total_vaccinations] => 8200
                            [people_vaccinated] => 8200
                            [daily_vaccinations] => 1367
                            [total_vaccinations_per_hundred] => 0.02
                            [people_vaccinated_per_hundred] => 0.02
                            [daily_vaccinations_per_million] => 35
                        )

                    [7] => Array
                        (
                            [date] => 2021-03-01
                            [daily_vaccinations] => 1580
                            [daily_vaccinations_per_million] => 41
                        )

                    [8] => Array
                        (
                            [date] => 2021-03-02
                            [daily_vaccinations] => 1794
                            [daily_vaccinations_per_million] => 46
                        )

                    [9] => Array
                        (
                            [date] => 2021-03-03
                            [daily_vaccinations] => 2008
                            [daily_vaccinations_per_million] => 52
                        )

                    [10] => Array
                        (
                            [date] => 2021-03-04
                            [daily_vaccinations] => 2221
                            [daily_vaccinations_per_million] => 57
                        )

                    [11] => Array
                        (
                            [date] => 2021-03-05
                            [daily_vaccinations] => 2435
                            [daily_vaccinations_per_million] => 63
                        )

                    [12] => Array
                        (
                            [date] => 2021-03-06
                            [daily_vaccinations] => 2649
                            [daily_vaccinations_per_million] => 68
                        )

                    [13] => Array
                        (
                            [date] => 2021-03-07
                            [daily_vaccinations] => 2862
                            [daily_vaccinations_per_million] => 74
                        )

                    [14] => Array
                        (
                            [date] => 2021-03-08
                            [daily_vaccinations] => 2862
                            [daily_vaccinations_per_million] => 74
                        )

                    [15] => Array
                        (
                            [date] => 2021-03-09
                            [daily_vaccinations] => 2862
                            [daily_vaccinations_per_million] => 74
                        )

                    [16] => Array
                        (
                            [date] => 2021-03-10
                            [daily_vaccinations] => 2862
                            [daily_vaccinations_per_million] => 74
                        )

                    [17] => Array
                        (
                            [date] => 2021-03-11
                            [daily_vaccinations] => 2862
                            [daily_vaccinations_per_million] => 74
                        )

                    [18] => Array
                        (
                            [date] => 2021-03-12
                            [daily_vaccinations] => 2862
                            [daily_vaccinations_per_million] => 74
                        )

                    [19] => Array
                        (
                            [date] => 2021-03-13
                            [daily_vaccinations] => 2862
                            [daily_vaccinations_per_million] => 74
                        )


Small snipped of the HTML/PHP table


    <div class="container my-3 text-center">
      <h2 class="display-6">Vaccine Live Data</h2>
      <table class="table sortable" id="table" data-toggle="table">
        <thead class="table-dark">
          <tr>
            <th scope="col">Countries</th>
            <th scope="col">Last Updated</th>
            <th scope="col">Total vaccinated</th>
            <th scope="col">Total Vaccinated Per Hundred</th>

          </tr>
        </thead>

        <tbody>
          <?php
                        foreach($data as $key => $info){
                    ?>
            <tr>
              <th class="sorttable_nosort">
                <?php echo $info['country']; ?>
              </th>

              <td data-order='desc' data-field="firstdose" data-sortable="true">
                <?php foreach($info['data'] as $key => $value) {  echo $value['date'];
     } ?>
              </td>

            </tr>

            <?php } ?>
        </tbody>



      </table>
    </div>

The current table output: image here

1 Answers

Try something like this, is not finished. but you can take an idea. Basically this script takes the last array of the country and display it, assuming that is the last updated. You can sort it with js table.

<div class="container my-3 text-center">
<h2 class="display-6">Vaccine Live Data</h2>
<table class="table sortable" id="table" data-toggle="table">
<thead class="table-dark">
    <tr>
    <th scope="col">Countries</th>
    <th scope="col">Last Updated</th>
    <th scope="col">Total vaccinated</th>
    <th scope="col">Total Vaccinated Per Hundred</th>
    <th scope="col">People Vaccinated Per Hundred</th>

    </tr>
</thead>

<tbody>
    <?php foreach($data as $key => $info){
        $last_array = end($info["data"]);
    ?>
    <tr>
        <td class="sorttable_nosort">
        <?php echo $info['country']; ?>
        </td>
        <td data-order='desc' data-field="firstdose" data-sortable="true">
            <?=$last_array["date"]?>
        </td>
        <td data-order='desc' data-field="firstdose" data-sortable="true">
            <?=$last_array["total_vaccinations"]?>
        </td>
        <td data-order='desc' data-field="firstdose" data-sortable="true">
            <?=$last_array["total_vaccinations_per_hundred"]?>
        </td>
        <td data-order='desc' data-field="firstdose" data-sortable="true">
            <?php
            if($last_array["people_vaccinated_per_hundred"]){
                echo $last_array["people_vaccinated_per_hundred"];
            }else{
                echo 'No data available';
            }
            ?>
        </td>

    </tr>

    <?php } ?>
</tbody>



</table>

enter image description here

Related