Jump to content

Need help deconstructing objects


fbords

Recommended Posts

Hi all,

 

I am working with the following 2 page script: https://github.com/kevinohashi/php-riot-api. All works well and the data is pulled via the API as intended. However, depending on the data I'm trying to display on screen, the object contains multiple values.

 

For example:

$r = $test->getSummoner($summoner_id,'name');
print_r($r);

outputs their ID and name: {"585897":"TheirName"}

 

Just as the following:

$r = $test->getSummonerByName($summoner_name);
print_r($r);

outputs username, ID, Name, profile icon ID, level, and timestamp:

{"theirname":{"id":585897,"name":"TheirName","profileIconId":642,"summonerLevel":30,"revisionDate":1405652924000}}

 

Depending on which function I am calling (on example.php), I would like to be able to split the values so I can echo them throughout a page:

 

Name: <value>

ID: <value>

Level: <value>

etc...

 

Any guidance would be appreciated.

Link to comment
Share on other sites

"Object"? So you've already decoded the JSON?

 

Well, your code is the one making the decision about which method to call, right? So... your code knows what format the data will come back in. So... the code that calls getSummoner() knows it only has the name to work with, and the code that calls getSummonerByName() knows it has additional information.

 

So... I guess I don't see where the problem is?

Link to comment
Share on other sites

the problem is likely with my lack of expertise on the subject. My issue is pertaining to the formatting of the results that are returned. The function dumps all of the requested data, I am looking to be able to selectively choose the data displayed.

 

So instead of it returning: {"username":{"id":585897,"name":"TheirName","profileIconId":642,"summonerLevel":30,"revisionDate":1405652924000}}

 

I would like to choose to just show the username, id, name, etc. 

Link to comment
Share on other sites

Assuming the functions are returning those strings, then you first need to decode them with json_decode. After that you can access the individual properties.

$r = $test->getSummonerByName($summoner_name);
$r = json_decode($r, true);
foreach ($r as $theirname=>$details){
  echo $theirname."\r\n";
  echo "ID: ".$details['id']."\r\n";
  echo "Level: ".$details['summonerLevel']."\r\n";
}
Link to comment
Share on other sites

Assuming the functions are returning those strings, then you first need to decode them with json_decode. After that you can access the individual properties.

$r = $test->getSummonerByName($summoner_name);
$r = json_decode($r, true);
foreach ($r as $theirname=>$details){
  echo $theirname."\r\n";
  echo "ID: ".$details['id']."\r\n";
  echo "Level: ".$details['summonerLevel']."\r\n";
}

 

I see. That's what I'm looking to do. What about functions with additional properties like:

$r = $test->getSummoner($summoner_id,'masteries');

How would i go about accessing the "masteries" properties? The following wont work. I can't quite get the syntax right:

$r = $test->getSummoner($summoner_id,'masteries');

$r = json_decode($r, true);
foreach ($r as $theirname=>$details){
  echo $theirname."\r\n";
  echo "Masteries: ".$details['masteries']."\r\n";
}
Link to comment
Share on other sites

i may be explaining this incorrectly. if so, i apologize, I'm still learning.

 

the above example works for:

$r = $test->getSummonerByName($summoner_name);

but i would like to be able to use it for:

$r = $test->getSummoner($summoner_id,'masteries');

"Masteries" is a nested property within $summoner_id. I'm trying to use kicken's decode method to echo the information but it won't work.

Link to comment
Share on other sites

You just need to do some reading about how to understand JSON then you'll know exactly how to access any property you might want to access.

 

json_decode($json, true); will turn the json string into a PHP array with the property names as array keys, so if you have the json:

$json = '{"myProperty": {"subProperty":"This is a value"} }';
you would access the property subProperty as:

$array = json_decode($json, true);
$value = $array['myProperty']['subProperty'];
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.