PHP面向对象的数组输出

时间:2014-08-24 17:56:17

标签: php arrays object echo

我自己没有制作阵列,所以老实说我不知道​​如何回应数组中的特定变量。

这是我的代码:

foreach($server_players as $data) {
    print_r($data);
}

哪个输出

SteamPlayer Object
(
    [clientPort:SteamPlayer:private] => 
    [connectTime:SteamPlayer:private] => 76.529731750488
    [extended:SteamPlayer:private] => 
    [id:SteamPlayer:private] => 0
    [ipAddress:SteamPlayer:private] => 
    [loss:SteamPlayer:private] => 
    [name:SteamPlayer:private] => Din yndlings Slyngel <3
    [ping:SteamPlayer:private] => 
    [rate:SteamPlayer:private] => 
    [realId:SteamPlayer:private] => 
    [score:SteamPlayer:private] => 0
    [state:SteamPlayer:private] => 
    [steamId:SteamPlayer:private] => 
)

var_dump输出

object(SteamPlayer)[14]
  private 'clientPort' => null
  private 'connectTime' => float 6556.9243164062
  private 'extended' => boolean false
  private 'id' => int 0
  private 'ipAddress' => null
  private 'loss' => null
  private 'name' => string 'Alfred' (length=6)
  private 'ping' => null
  private 'rate' => null
  private 'realId' => null
  private 'score' => int 2
  private 'state' => null
  private 'steamId' => null

我想把不同玩家的玩家名称,但我不知道如何。

这是我想做的事情

foreach($server_players as $data) {
    echo "Player name: ".$data['name:SteamPlayer:private'];
}

3 个答案:

答案 0 :(得分:1)

试试这个:

foreach($server_players as $data) {
  echo "Player name: ".$data->getName();
}

答案 1 :(得分:0)

上述答案的路径正确,但正如您的输出所示:

[name:SteamPlayer:private]

因此,您需要了解对象中是否有吸气剂,或者您必须将某些字段更改为公开。

如果您不能做其中任何一项,那么根据您提供的信息,没有直接的方法可以做到这一点。

答案 2 :(得分:-1)

您可以尝试这样做

foreach($server_players as $data) {
    echo "Player name: ".$data->name;
}