3

鉴于此 stdClass 结构...

[RecentAchievements] => stdClass Object
    (
        [1446] => stdClass Object
           (
               [3319] => stdClass Object
                   (
                       [ID] => 3319
                       [GameID] => 1446
                       [Title] => Hello World!
                       [Description] => Complete World 1-1
                       [Points] => 15
                       [BadgeName] => 03909
                       [IsAwarded] => 1
                       [DateAwarded] => 2013-10-20 19:35:46
                   )

我需要检查 3319 里面的对象,比如“标题”,所以它就像:

$data->RecentAchievements->$gameid->ACHVIDHERE->Title;

要获取 $gameid,它来自其他对象(RecentlyPlayed),这样:

[RecentlyPlayed] => Array
    (
        [0] => stdClass Object
            (
                [GameID] => 1446
                [ConsoleID] => 7
                [ConsoleName] => NES
                [Title] => Super Mario Bros
                [LastPlayed] => 2013-10-20 21:38:22
                [ImageIcon] => /Images/000385.png
                [ImageTitle] => /Images/000272.png
                [ImageIngame] => /Images/000387.png
                [ImageBoxArt] => /Images/000275.png
            )

    )

$gameid = $data3->RecentlyPlayed['0']->GameID;

API不允许我使用相同的RecentPlayed 来获取成就ID,我必须从RecentAchievements 中提取它。是否可以?

4

2 回答 2

5

如果您刚刚获得该特定游戏的第一个成就,则可以使用以下技巧:

$firstAchievement = reset($data->RecentAchievements->$gameid);
$title = $firstAchievement->Title;

API 可能会为您提供 JSON 响应,因此您可能还需要考虑这一点:

$data = json_decode($response, true);
                               ^^^^

第二个参数true将 JavaScript 对象转换为 PHP 数组,这在大多数情况下更容易处理它们,例如

$firstAchievement = reset($data['RecentAchievements'][$gameid]);
$title = $firstAchievement['Title'];
于 2013-10-21T00:57:25.977 回答
1

您可以使用复杂的大括号来引用对象的动态属性

$data->RecentAchievements->{$gameid}->{$someotherid}->Title;

这应该给你Hello World!,给定$gameid==1446$someotherid==3319

于 2013-10-21T00:23:50.833 回答