Navigating through api response in php

I am trying to to pull data from an API response. but can't seem to figure out how to navigate to the "statistics" section of the response.

Here is the responce in short

{
   "response": [
       {
          "player": {
          },
          "statistics": [
             {
                "team": {
                   "id": 49,
                   "name": "Chelsea",
                   "logo": "some data"
                }
             }
          ]
       }
   ]
}

The code I have at the moment is as follows:

$leaguelist = array();
if (! empty( $desc-> response)) {
    foreach ($desc->response as $players){
          $player['id'] = $players->player->id;
          $player['name'] = $players->player->name;
          $player['first_name'] = $players->player->firstname;
          $player['last_name'] = $players->player->lastname;
          $player['age'] = $players->player->age;
          $player['dob'] = $players->player->birth->date;
          $player['pob'] = $players->player->birth->place;
          $player['cob'] = $players->player->birth->country;
          $player['nationality'] = $players->player->nationality;
          $player['height'] = $players->player->height;
          $player['weight'] = $players->player->weight;
          $player['photo'] = $players->player->photo;
          $player['team_logo'] = $players->statistics->team->logo;
          $leaguelist[] = $player;
    }
}

I am able to pull and display all data from the player directory just having problems working out the code to get onto the statistics

I have tried $player['team_logo'] = $players->statistics->team->logo;

I can't really research much into this as I don't know what "this" is called, any help would be great as i am a hobbyist and have ran out of ideas.

Thank you in advance

Answer

Solution:

Assuming that the response you show is in JSON, and you've then parsed it with {-code-24}-code-21}-code-14}-code-8}-code-1}, there are only two types of structure you need to know about:

  • Anything in {-code-24}-code-21}-code-14}-code-8}-code-2} square brackets is an array, and is accessed as numbered items {-code-24}-code-21}-code-14}-code-8}-code-3}, {-code-24}-code-21}-code-14}-code-8}-code-4}, etc; or with a {-code-24}-code-21}-code-14}-code-8}-code-5} loop
  • Anything in {-code-24}-code-21}-code-14}-code-8}-code-6} curly brackets is an object, and is accessed using {-code-24}-code-21}-code-14}-code-8}-code-7}, etc

So working from the outside, we have:

  1. An outer object {-code-24}-code-21}-code-14}-code-8} which you've called {-code-24}-code-21}-code-14}-code-9}
  2. ... from which we want key {-code-24}-code-21}-code-14}-code-10}: {-code-24}-code-21}-code-14}-code-9}->response ...
  3. Then an array {-code-24}-code-21}-code-14}-code-12}, which you've looped over: {-code-24}-code-21}-code-14}-code-8}-code-5} ({-code-24}-code-21}-code-14}-code-9}->response as {-code-24}-code-21}-code-15})
  4. Then in the first item of that array, an object {-code-24}-code-21}-code-14}-code-8} which the loop assigns to {-code-24}-code-21}-code-15} ...
  5. ... from which we want key {-code-24}-code-21}-code-16}: {-code-24}-code-21}-code-15}->statistics ...
  6. Then an array {-code-24}-code-21}-code-14}-code-12} ...
  7. ... from which we could take the first item: $stat = {-code-24}-code-21}-code-15}->statistics{-code-24}-code-21}-code-14}-code-8}-code-3}; or loop over all the items: {-code-24}-code-21}-code-14}-code-8}-code-5} ( {-code-24}-code-21}-code-15}->statistics as $stat )
  8. Each item is then an object {-code-24}-code-21}-code-14}-code-8} ...
  9. ... from which we want the {-code-24}-code-22} key: {-code-24}-code-23}
  10. Which is an object {-code-24}-code-21}-code-14}-code-8} ...
  11. ... from which we want the {-code-25} key: {-code-24}-code-23}->id

If we just wanted that one value, we could write it all in one go: {-code-24}-code-21}-code-14}-code-9}->response{-code-24}-code-21}-code-14}-code-8}-code-3}->statistics{-code-24}-code-21}-code-14}-code-8}-code-3}->team->id. It doesn't matter how deep we're nesting, we just need to look for the {-code-24}-code-21}-code-14}-code-12} or {-code-24}-code-21}-code-14}-code-8} to see what to do next.

Answer

Solution:

statistics is an array. So if you want an item from within it, you need to refer to an index of that array which contains the item.

E.g.

$player['team_logo'] = $players->statistics[0]->team->logo;

to get the first item.

Source