2017-10-08 73 views
1

我有一個stdClass物體看起來像這樣,我想echosteamid如何在stdClass對象中訪問此字符串?

stdClass Object 
(
    [response] => stdClass Object 
     (
      [players] => Array 
       (
        [0] => stdClass Object 
         (
          [steamid] => 76561198039509812 
          [communityvisibilitystate] => 1 
          [profilestate] => 1 
          [personaname] => Mike_Ock_Hurtz 
          [lastlogoff] => 1506899637 
          [profileurl] => http://steamcommunity.com/profiles/76561198039509812/ 
          [avatar] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg 
          [avatarmedium] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg 
          [avatarfull] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg 
          [personastate] => 0 
         ) 

       ) 

     ) 

) 

下面是我如何得到上面:

foreach(updateBanList::readFile($file) as $steamprofiles){ 
    $steam = $steamUser->GetPlayerSummariesV2($steamprofiles[0]); 
    print_r($steam); 
    $players = $steam->response->players; 


} 

迄今訪問類的我已使用

$players = $steam->response->players; 

這讓我到最後array

Array 
(
    [0] => stdClass Object 
     (
      [steamid] => 76561198039509812 
      [communityvisibilitystate] => 1 
      [profilestate] => 1 
      [personaname] => Mike_Ock_Hurtz 
      [lastlogoff] => 1506899637 
      [profileurl] => http://steamcommunity.com/profiles/76561198039509812/ 
      [avatar] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg 
      [avatarmedium] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg 
      [avatarfull] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg 
      [personastate] => 0 
     ) 

) 

我想echosteamid,我嘗試了幾件事情,我知道不管它是我做的是非常簡單的,但其在尾盤,我無法弄清楚。

如果我嘗試

$players->steamid; 

我得到的錯誤

注意:試圖如果我嘗試

$players['steamid']; 

獲得非對象

財產我明白了

注意:未定義指數:steamid

我在做什麼錯?

回答

2

嘗試$players[0]->steamid; 你的對象是一個數組

內,如果你相信你會只有數組中得到一個對象元素,然後使用

$player = $steam->response->players[0]; 

$player->steamid;就足夠了。

0

$players是對象的索引數組。

試試這個:

print_r($players[0]->steamid); 

還是這個傾倒所有的數組元素:

foreach ($players as $player) { 
    print_r($player->steamid); 
} 
1

你必須使用數組。

$players = $steam->response->players; 
$steamid = $players[0]->steamid; 

或在一行中;

$steamid = $steam->response->players[0]->steamid; 

現在你可以回聲出你steamid:

echo $steamid; 

如果您的陣列在一個以上的球員,你必須循環你:

$players = $steam->response->players; 
$steamids = array(); 

foreach ($players as $key => $player) 
{ 
    $steamids[$key] = $player->steamid; 
} 

現在你有一個數組$steamids的Steam ID信息。爲了呼應它,你可以這樣做:

foreach ($steamids as $key => $steamid) 
{ 
    echo "Player " . $key . " has Steam ID:" . $steamid; 

} 

注意:試圖讓非對象

的財產現在你明白,未來的數據類型是一個數組,而不是一個對象,它很明顯,做類似$players->steamid;會導致錯誤。

注意:未定義指數:steamid

$players['steamid'];雲也無法工作,因爲沒有鑰匙/索引steamid這個數組只有一個主鍵/索引與價值0

0

$players是數組,您應該訪問它對數組元素的屬性:

$players[0]->steamid;