fbords Posted July 18, 2014 Share Posted July 18, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/ Share on other sites More sharing options...
requinix Posted July 18, 2014 Share Posted July 18, 2014 "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? Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1485628 Share on other sites More sharing options...
fbords Posted July 18, 2014 Author Share Posted July 18, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1485640 Share on other sites More sharing options...
kicken Posted July 18, 2014 Share Posted July 18, 2014 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1485647 Share on other sites More sharing options...
fbords Posted July 18, 2014 Author Share Posted July 18, 2014 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1485673 Share on other sites More sharing options...
fbords Posted July 23, 2014 Author Share Posted July 23, 2014 any input on this? Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1486003 Share on other sites More sharing options...
requinix Posted July 23, 2014 Share Posted July 23, 2014 How would i go about accessing the "masteries" properties?What property? I don't see it mentioned anywhere else in this thread. Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1486030 Share on other sites More sharing options...
fbords Posted July 27, 2014 Author Share Posted July 27, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1486259 Share on other sites More sharing options...
kicken Posted July 27, 2014 Share Posted July 27, 2014 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']; Quote Link to comment https://forums.phpfreaks.com/topic/289969-need-help-deconstructing-objects/#findComment-1486270 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.