2014-10-17 99 views
0

如果我的数组声明:如何在PHP中显示数组中的信息?

[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; 

谢谢你的时间。

+0

'{$ user-> mostPlayedGames [0] - > hoursOnRecord}' – 2014-10-17 04:19:06

回答

0

您的mostPlayedGames阵列看起来好像不是stdClass的任何变量名为$user,所以我们不要让它复杂化。

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

现在我们已经清楚了:

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

你看,在这第一个元素是array0位置,所以我们必须先移动到该索引位置。索引0上的元素是stdClass,因此我们可以使用访问器方法->来获取此对象的属性。

+0

对不起,我遗漏了大部分我正在使用的代码。添加0修复了我遇到的问题。感谢您的帮助。 – project 2014-10-17 04:22:16