-1

I get the following 2 arrays from an Api using php. How can i display the "comp" and home_title for each array using php?

  "history": [
    {
      "comp": "comp_name_1",
      "season": "2021",
      "id": "4999999",
      "statscore": null,
      "home_id": "1452",
      "away_id": "5467",
      "date_played": "2021-07-07",
      "time_played": "20:00:00",
      "fixture": "1",
      "court": null,
      "seats": null,
      "distance": null,
      "home_title": "team_1",
      "away_title": "team_2"
    },
    {
      "comp": "comp_name_2",
      "season": "2020",
      "id": "470073",
      "statscore": "3777117",
      "home_id": "1452",
      "away_id": "5467",
      "date_played": "2020-09-17",
      "time_played": "20:00:00",      
      "home_title": "team_1",
      "away_title": "team_2"
    }
  ],
John Bill
  • 3
  • 1
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Tijmen Jul 08 '21 at 09:03
  • Welcome to the site. Please use the search function to see whether your question already has an answer. This particular question has been asked and answered here dozens if not hundreds of time. For example, you might want to take a look at this question and answer: [how do I extract data from json with php](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Tijmen Jul 08 '21 at 09:05
  • What have you tried so far? Where are you stuck? – Nico Haase Jul 08 '21 at 09:08

1 Answers1

0

You can use any html tags in the output, inside the loop.

 $apiData = json_decode($yourDataFromApi, true);
    
    foreach ($apiData['history'] as $item) {
        //Any html tags, as example tag <p>
        echo '<p>' . $item['comp'] . ' ' . $item['home_title'] . '</p>';
    }
Alex Black
  • 450
  • 2
  • 7