law Posted June 28, 2012 Share Posted June 28, 2012 This works and is tested. PHP Code: $data_stats = json_decode($ret); $projected_points = $data_stats->body->player_stats->42550689->FPTS I just need to know how to do it using a variable. Something like this: PHP Code: $data_stats = json_decode($ret); $projected_points = $data_stats->body->player_stats->$id->FPTS How can I make $id work here? Also is this called extending a class? I don't even know the correct terminology to describe my question. Quote Link to comment https://forums.phpfreaks.com/topic/264966-extending-a-class-with-a-variable/ Share on other sites More sharing options...
KevinM1 Posted June 28, 2012 Share Posted June 28, 2012 No, extending a class is a completely different thing. Can you give us the print_r of $data_stats? Quote Link to comment https://forums.phpfreaks.com/topic/264966-extending-a-class-with-a-variable/#findComment-1357797 Share on other sites More sharing options...
DavidAM Posted June 28, 2012 Share Posted June 28, 2012 Hm, I'm surprised it works. I did not think that methods or properties could start with a digit. However, to use a variable in that expression, you would put it in curly braces: $projected_points = $data_stats->body->player_stats->{$id}->FPTS I've used it with property names and method calls, but never in the middle of a chain like that, so YMMV. Oh, and NO, this is not what is meant by "extending a class" (unless I am really out of the loop here). Quote Link to comment https://forums.phpfreaks.com/topic/264966-extending-a-class-with-a-variable/#findComment-1357798 Share on other sites More sharing options...
law Posted June 28, 2012 Author Share Posted June 28, 2012 KevinM1 here is a snippet of the print r (it contains 100,000+ entries): stdClass Object ( [body] => stdClass Object ( [player_stats] => stdClass Object ( [1749143] => stdClass Object ( [_empty_] => 0 [TM] => SEA [FPTS] => 0 [GS] => 16.0 [G] => 16.0 [Pos] => LB ) [1616752] => stdClass Object ( [_empty_] => 0 [CmpPct] => 0 [FPTS] => 0 [RuAvg] => 0 [YAtt] => 0 [GS] => 16.0 [Pos] => RB [TM] => PIT [ReAvg] => 0 [G] => 16.0 ) My goal is to access these ids to pull the FPTS out. DavidAM: I am getting a T_VARIABLE issue. How should I describe this question? Sorry for the ignorance. Quote Link to comment https://forums.phpfreaks.com/topic/264966-extending-a-class-with-a-variable/#findComment-1357803 Share on other sites More sharing options...
DavidAM Posted June 28, 2012 Share Posted June 28, 2012 I would just json_decode it as an array instead of as objects. Then you can reference them as array keys: $data_stats = json_decode($ret, true); $projected_points = $data_stats['body']['player_stats'][$id]['FPTS']; Quote Link to comment https://forums.phpfreaks.com/topic/264966-extending-a-class-with-a-variable/#findComment-1357807 Share on other sites More sharing options...
law Posted June 28, 2012 Author Share Posted June 28, 2012 Thank you DavidAM, I found the error was due to my stupidity at checking the cleanliness of my code. Both your array suggestion and $projected_points = $data_stats->body->player_stats->{$id}->FPTS; work and are very helpful. Thank you guys for the help. Quote Link to comment https://forums.phpfreaks.com/topic/264966-extending-a-class-with-a-variable/#findComment-1357808 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.