Reputation: 128
Good day,
I'm fetching my data directly from and API endpoint. I was able to get the id
but how can I access "type":"participant",
specifically the skillTier
?
Here's my code but it's throwing an error: NOTICE: UNDEFINED INDEX: SKILLTIER IN C:\XAMPP\HTDOCS\VGAPP\INDEX.PHP ON LINE 219
if(is_array($finaljson) || is_object($finaljson)){
if(!empty($finaljson['included']) && $finaljson['included'] !== ""){
foreach ($finaljson['included'] as $key => $value) {
if(!empty($value['attributes']['name']) && $value['attributes']['name'] === $username){
$userid = $value['id'];
$skillTier = $value['skillTier'];
}
}
Here is the json data:
https://gist.github.com/kamaelxiii/18ef0c1600330b98717b96db00532d6a
Upvotes: 2
Views: 107
Reputation: 47894
It is patient work to whittle down a big json / array to target and isolate your desired data. You just need to traverse the structure step-by-step, print each level's data to screen, and identify the keys that hold what you want.
Code:
$array = json_decode($json, true);
foreach ($array['included'] as $included) { // iterate all children of "included"
if ($included['type'] === 'participant') { // only access "participant" subset data
$result[$included['id']] = $included['attributes']['stats']['skillTier']; // access the skillTier data which is beneath "stats" beneath "attributes"
}
}
echo "<pre>";
var_export($result);
echo "</pre>";
Output:
array (
'4cfe4736-f47b-11e6-b633-0242ac110030' => 16,
'4cfe473f-f47b-11e6-b633-0242ac110030' => 10,
'4cfe4727-f47b-11e6-b633-0242ac110030' => 27,
'd4003883-f423-11e6-865e-0242ac110008' => 23,
'd4003869-f423-11e6-865e-0242ac110008' => 24,
'd400387a-f423-11e6-865e-0242ac110008' => 23,
'4cfe471f-f47b-11e6-b633-0242ac110030' => 27,
'd400386d-f423-11e6-865e-0242ac110008' => 24,
'd4003887-f423-11e6-865e-0242ac110008' => 23,
'4cfe4724-f47b-11e6-b633-0242ac110030' => 24,
'd4003864-f423-11e6-865e-0242ac110008' => 18,
'4cfe473b-f47b-11e6-b633-0242ac110030' => 17,
)
Upvotes: 1
Reputation: 2581
First of all you have 2 PHP syntax errors where you have two missing }
's to close off your first if
statement and foreach
loop.
Also you want to loop your included
nodes and check if you have a participant
object before continuing since the other items don't have the skillTier value. Also best practice to check if the skilltier isn't empty before trying to save it in a variable.
This is the updated version of your code:
if(is_array($finaljson) || is_object($finaljson)) {
foreach($finaljson['included'] as $included) {
if($included['type'] === 'participant') {
if(!empty($included['attributes']['stats']['skillTier'])){
$id = $included['id'];
$skillTier = $included['attributes']['stats']['skillTier'];
}
}
}
}
Here is a working example with your JSON that will var_dump the ID's and skilltier. http://ideone.com/DrXWe7 (click fork to see the code)
Upvotes: 1
Reputation: 71
foreach($finaljson['included'] as $included) {
if($included['type'] == 'participant'){
$userid = $included['id'];
$skillTier = $included['attributes']['stats']['skillTier'];
}
}
Upvotes: 1
Reputation: 8288
As the same way that you've accessed the $value['attributes']['name']
, it's multi-dimensional array , so think about it as a town , and you want to arrive to flat number 6 which is at building 9 , which is located at street number 4 , you will not be able to ask somebody to tell you where is flat 6 without telling the full address , so in your case , you simply can achieve your skillTier
index when only you provide your variable with the full address of your index , as follows :
$value['attributes']['stats]['skillTier']
Upvotes: 1