如何从数组中获取数据

时间:2013-11-12 16:10:56

标签: php arrays

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

这是数组:

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

1 个答案:

答案 0 :(得分:2)

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

[gameType]似乎是此“游戏”object的受保护属性。您可以 通过$array['game']->gameType访问此受保护的属性,除非“游戏”具有魔术方法__get()或访问者方法$array['game']->getGameType() 1}}。

试试这个以查看可用内容:

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