2014-10-17 101 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