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
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:
{-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{-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}
, etcSo working from the outside, we have:
{-code-24}-code-21}-code-14}-code-8}
which you've called{-code-24}-code-21}-code-14}-code-9}
{-code-24}-code-21}-code-14}-code-10}
:{-code-24}-code-21}-code-14}-code-9}->response
...{-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})
{-code-24}-code-21}-code-14}-code-8}
which the loop assigns to{-code-24}-code-21}-code-15}
...{-code-24}-code-21}-code-16}
:{-code-24}-code-21}-code-15}->statistics
...{-code-24}-code-21}-code-14}-code-12}
...$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 )
{-code-24}-code-21}-code-14}-code-8}
...{-code-24}-code-22}
key:{-code-24}-code-23}
{-code-24}-code-21}-code-14}-code-8}
...{-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.
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.