Jump to content

Extending a class with a variable?


law

Recommended Posts

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.