-3

我正在尝试从数组中获取数据,但总是失败。

这是数组:

Array
(
    [gameSpecificLoyaltyRewards] => 
    [reconnectDelay] => 0
    [lastModifiedDate] => 
    [game] => Game Object
        (
            [spectatorsAllowed:protected] => NONE
            [passwordSet:protected] => 
            [practiceGameRewardsDisabledReasons:protected] => Array
                (
                )

            [gameType:protected] => RANKED_GAME
            [gameTypeConfigId:protected] => 2
            [glmGameId:protected] => 
            [gameState:protected] => IN_PROGRESS
            [glmHost:protected] => 
            [observers:protected] => Array
                (
                )

            [statusOfParticipants:protected] => 1111111111
            [glmSecurePort:protected] => 0
            [id:protected] => 58513543
            [ownerSummary:protected] => 
            [teamTwo:protected] => Array
                (
                    [0] => zlokomatic\phpLoL\amf\game\PlayerParticipant Object
                        (
                            [accountId:protected] => 346001
                            [queueRating:protected] => 0
                            [botDifficulty:protected] => NONE
                            [minor:protected] => 
                            [locale:protected] => 
                            [partnerId:protected] => 
                            [lastSelectedSkinIndex:protected] => 0
                            [profileIconId:protected] => 548
                            [rankedTeamGuest:protected] => 
                            [summonerId:protected] => 361101
                            [selectedRole:protected] => 
                            [pickMode:protected] => 0
                            [teamParticipantId:protected] => 138114436
                            [timeAddedToQueue:protected] => 1384270965374

使用PHP,我试图得到:

[gameType:protected] => RANKED_GAME

[accountId:protected] => 346001
4

1 回答 1

2

$array['game']似乎是object“游戏”类型。

[gameType]似乎是此“游戏”的受保护财产object。除非“游戏”具有魔术方法或访问器方法,否则您可能无法访问此受保护的属性。$array['game']->gameType__get()$array['game']->getGameType()

试试这个看看有什么可用的:

print_r( get_class_methods($array['game']) );

编辑,快速演示:

class Game {
  protected $gameType = 'RANKED_GAME';

  public function __get($x) {
    return $this->$x;
  }

  public function getGameType() {
    return $this->gameType;
  }
}

$array = array(
  'game' => new Game,
);

你基本上看到的是:

print_r($array);

Array
(
  [game] => Game Object
    (
        [gameType:protected] => RANKED_GAME
    )
)

没有可用的__get()魔术方法:

echo $array['game']->gameType;

// Fatal error: Cannot access protected property Game::$gameType

__get()

echo $array['game']->gameType;

// RANKED_GAME

echo $array['game']->getGameType();

// RANKED_GAME
于 2013-11-12T16:32:55.303 回答