简体   繁体   English

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

[英]How can I access this string in an stdClass object?

I have an stdClass object which looks like this, I am trying to echo out the steamid 我有一个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
                        )

                )

        )

)

Below is how I am getting the above: 以下是我如何获得上述内容:

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


} 

To access the class so far I am have used 到目前为止访问该课程我已经习惯了

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

This gets me to the last array 这让我到最后一个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
        )

)

I am trying to echo out the steamid , and I've tried a few things I know whatever it is i am doing is very simple, but its late in the day and I cannot figure it out. 我试图echo steamid ,我已经尝试了一些我知道的事情,我正在做的事情很简单,但它在当天晚些时候,我无法弄明白。

If I try 如果我试试

$players->steamid;

I get the error 我收到了错误

Notice: Trying to get property of non-object 注意:尝试获取非对象的属性

If I try 如果我试试

$players['steamid'];

I get 我明白了

Notice: Undefined index: steamid 注意:未定义的索引:steamid

What am I doing wrong? 我究竟做错了什么?

Try $players[0]->steamid; 试试$players[0]->steamid; Your object is inside an array 您的对象位于数组中

And if you are sure you will only get one object element in the array then use 如果您确定只在数组中获得一个对象元素,那么请使用

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

$player->steamid; will suffice. 就足够了。

You have to use the array. 你必须使用该数组。

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

or in one line; 或一行;

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

Now you can echo out you steamid: 现在你可以回应你蒸汽:

echo $steamid;

If you have more than one Player in your array you have to loop thou: 如果阵列中有多个播放器,则必须循环播放:

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

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

Now you have an array of Steam IDs in $steamids . 现在你在$steamids有一系列Steam ID。 To echo it you can do: 为了回应它你可以做:

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

}

Notice: Trying to get property of non-object 注意:尝试获取非对象的属性

Now that you understand that the next data type is an array and not an object, it is clear that doing something like $players->steamid; 现在您已经了解下一个数据类型是一个数组而不是一个对象,很明显,像$players->steamid; will cause an Error. 会导致错误。

Notice: Undefined index: steamid 注意:未定义的索引:steamid

$players['steamid']; cloud also not work because there is no Key/Index steamid in this array only one Key/Index with the value 0 云也不起作用,因为此数组中没有Key / Index steamid只有一个值为0 Key / Index

$players is an indexed array of objects. $players是一个索引的对象数组。

Try this: 尝试这个:

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

Or this to dump all the array elements: 或者这将转储所有数组元素:

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

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

$players[0]->steamid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM