-3

I'm very new to this and I need help. I have been searching and searching for days on end. My question is how to get it so my code I have here, returns just the values of one variable from inside the different object(stdClass)'s. Like I said I am very new and inexperienced with any sort of programming but I have self taught myself quite a bit.

Here is my current php code:

$rc = get_data('https://xboxapi.com/v2/2535433396471499/friends');
$rc2 = json_decode($rc);

Here is what I get when it is printed in the browser:

object(stdClass)#1 (13) {
["id"]=>
int(2535472804586102)
["hostId"]=>
NULL
["Gamertag"]=>
string(11) "Unikornz119"
["GameDisplayName"]=>
string(11) "Unikornz119"
["AppDisplayName"]=>
string(11) "Unikornz119"
["Gamerscore"]=>
int(3190)
["GameDisplayPicRaw"]=>
string(5) "a.png"
["AppDisplayPicRaw"]=>
string(180) "b.png"
["AccountTier"]=>
string(4) "Gold"
["XboxOneRep"]=>
string(10) "GoodPlayer"
["PreferredColor"]=>
string(0) ""
["TenureLevel"]=>
int(1)
["isSponsoredUser"]=>
bool(false)
}

Thank you all, I hope to hear some positive responses! :)

1 Answers1

0

Object properties are accessed like such:

$obj->property;

Array values are accessed like such:

$arr['key'];

So in your case, if you wanted to know the gamertag you'd look at $rc2->Gamertag

Alternatively, you can also tell json_decode to make the result an array by setting the second parameter of the function to true.

$rc = get_data('https://xboxapi.com/v2/2535433396471499/friends');
$rc2 = json_decode($rc, TRUE);

Then $rc2 would have an array and you could access $rc2['Gamertag'] instead.

skrilled
  • 5,350
  • 2
  • 26
  • 48