gpt4 book ai didi

php - 按值获取 stdClass 对象

转载 作者:可可西里 更新时间:2023-11-01 00:45:44 27 4
gpt4 key购买 nike

鉴于此 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 不允许我使用相同的 RecentlyPlayed 来获取成就 ID,我必须从 RecentAchievements 中提取它。可能吗?

最佳答案

如果您只是想获得该特定游戏的第一个成就,您可以使用此 hack:

$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'];

关于php - 按值获取 stdClass 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19484467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com