如何在stdClass对象中访问此字符串?

时间:2017-10-08 17:36:40

标签: php object stdclass

我有一个stdClass对象看起来像这样,我正试图echo steamid

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
        )

)

我正试图echo steamid,我尝试了一些我知道的事情,我正在做的事情非常简单,但它在当天晚些时候我无法想象它出来了。

如果我尝试

$players->steamid;

我收到错误

  

注意:尝试获取非对象的属性

如果我尝试

$players['steamid'];

我得到了

  

注意:未定义的索引:steamid

我做错了什么?

4 个答案:

答案 0 :(得分:2)

试试$players[0]->steamid; 您的对象位于数组

如果您确定在数组中只能获得一个对象元素,那么请使用

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

$player->steamid;就足够了。

答案 1 :(得分:1)

你必须使用数组。

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

或一行;

$steamid = $steam->response->players[0]->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

答案 2 :(得分:0)

$players是一个索引的对象数组。

试试这个:

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

或者这可以转储所有数组元素:

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

答案 3 :(得分:0)

$players是数组,你应该在数组元素上访问它的属性:

$players[0]->steamid;