简体   繁体   English

按值获取 stdClass 对象

[英]Get stdClass Object by value

Given this stdClass structure...鉴于这个 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
                   )

I need to check the objects inside 3319, like "Title", so it'd be like:我需要检查 3319 里面的对象,比如“标题”,所以它就像:

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

To get the $gameid, it came from other object (RecentlyPlayed), this way:要获取 $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 don't let me use the same RecentlyPlayed to get the achievement ID, i have to extract it from RecentAchievements. API 不允许我使用相同的最近播放来获取成就 ID,我必须从最近的成就中提取它。 Is it possible?有可能吗?

If you're just after the first achievement for that particular game, you could use this hack:如果您刚刚获得该特定游戏的第一个成就,则可以使用以下技巧:

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

The API is probably giving you a JSON response, so you may also want to consider this: API 可能会为您提供 JSON 响应,因此您可能还需要考虑这一点:

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

The second parameter true turns the JavaScript objects into PHP arrays, which in most cases makes it easier to handle them, eg第二个参数true将 JavaScript 对象转换为 PHP 数组,这在大多数情况下更容易处理它们,例如

$firstAchievement = reset($data['RecentAchievements'][$gameid]);
$title = $firstAchievement['Title'];

You can use complex curly notation to reference dynamic properties of an object您可以使用复杂的卷曲符号来引用对象的动态属性

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

This should give you Hello World!这应该会给你Hello World! , given $gameid == 1446 and $someotherid == 3319 , 给定$gameid == 1446$someotherid == 3319

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM