如何在PHP中显示数组中的信息?

时间:2014-10-17 04:15:29

标签: php arrays

如果我的数组说明:

[mostPlayedGames] => Array ( [0] => stdClass Object ( [gameName] => Counter-Strike: Global Offensive [gameLink] => http://steamcommunity.com/app/730 [gameIcon] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/69f7ebe2735c366c65c0b33dae00e12dc40edbe4.jpg [gameLogo] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/d0595ff02f5c79fd19b06f4d6165c3fda2372820.jpg [gameLogoSmall] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/d0595ff02f5c79fd19b06f4d6165c3fda2372820_thumb.jpg [hoursPlayed] => 28.0 [hoursOnRecord] => 527 [statsName] => CSGO ) [1] => stdClass Object ( [gameName] => Borderlands: The Pre-Sequel [gameLink] => http://steamcommunity.com/app/261640 [gameIcon] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/af5ef05eac8b1eb618e4f57354ac7b3e918ab1bd.jpg [gameLogo] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/df64c72fd335a03dbcc0a19b1f81acc8db1b94ba.jpg [gameLogoSmall] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/df64c72fd335a03dbcc0a19b1f81acc8db1b94ba_thumb.jpg [hoursPlayed] => 10.9 [hoursOnRecord] => 10.9 [statsName] => 261640 )

我希望显示数组第一部分的信息(0),如果我使用这样的代码来显示它,我该如何做呢:

echo "CS:GO Hours Played: {$user->mostPlayedGames???}, PHP_EOL;

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您的mostPlayedGames数组似乎不是名为stdClass的任何变量的$user,因此请不要过于复杂。

$mostPlayedGames = [mostPlayedGames] => Array ( [0] => stdClass Object ( [gameName]....[snippet]

现在我们已经明确了:

echo "CS:GO Hours Played: ${mostPlayedGames[0]->hoursPlayed}".PHP_EOL;

你知道,这里的第一个元素是位置array的{​​{1}}所以我们必须先移动到那个索引位置。索引0处的元素是0,因此我们可以使用访问器方法stdClass来获取此对象的属性。