我目前正在使用 Riot API 编写一个小接口。为此,我尝试以面向对象的方式在数组中正确过滤来自 API 的数据。到目前为止一切都合适,除了每场比赛创建一个数组来显示谁玩过哪个冠军。
我敢在这里进行初步尝试,但不幸的是我没有进一步尝试,我可能需要帮助。
这是我的启动尝试:
public function get_matches( $summoner_name ) {
$sliced_match_ids = $this->get_match_array( $summoner_name );
$continue_match_processing = true;
foreach ( $sliced_match_ids as $match_id ) {
$array = array();
$data = $this->execute_command( '/lol/match/v4/matches/' . $match_id );
foreach ( $data->participantIdentities as $key_identities => $part ) {
if ( ! isset( $part->player ) ) {
$continue_match_processing = false;
break;
}
if ( $part->player->summonerName == $summoner_name ) {
$participant_identity = $part->participantId; //phpcs:ignore
}
}
if ( ! $continue_match_processing ) {
continue;
}
foreach ( $data->participants as $participant ) {
if ( $participant->participantId == $participant_identity ) {
$match_array = array();
$match_array['gameDay'] = $data->gameCreation;
$match_array['gameMode'] = $data->gameMode;
$match_array['match_id'] = $match_id;
$match_array['win'] = $participant->stats->win;
$match_array['kills'] = $participant->stats->kills;
$match_array['deaths'] = $participant->stats->deaths;
$match_array['assists'] = $participant->stats->assists;
$match_array['gold'] = $participant->stats->goldEarned;
$match_array['totalMinions'] = $participant->stats->totalMinionsKilled;
$match_array['item0'] = $participant->stats->item0;
$match_array['item1'] = $participant->stats->item1;
$match_array['item2'] = $participant->stats->item2;
$match_array['item3'] = $participant->stats->item3;
$match_array['item4'] = $participant->stats->item4;
$match_array['item5'] = $participant->stats->item5;
$match_array['item6'] = $participant->stats->item6;
$match_array['spell1'] = $participant->spell1Id;
$match_array['spell2'] = $participant->spell2Id;
$match_array['championId'] = $participant->championId;
$match_array['gameDuration'] = $data->gameDuration;
$match_array['primaryRunes'] = $participant->stats->perkPrimaryStyle;
$match_array['secondaryRunes'] = $participant->stats->perkSubStyle;
$match_array['perk'] = $participant->stats->perk0;
$match_array['level'] = $participant->stats->champLevel;
$match_array['controlWard'] = $participant->stats->visionWardsBoughtInGame;
foreach ( $data->teams as $key_team => $value_team ) {
if ( $participant->teamId == $value_team->teamId ) {
foreach ( $data->participantIdentities as $key_participant => $value_participant ) {
$match_array['teams'][ $value_team->teamId ] = array( $value_participant->player->summonerName );
}
}
}
$match_list[] = $match_array;
}
}
}
return $match_list;
}
该数组确保输出召唤师每次匹配的所有数据。在此,我还想在每场比赛中使用 Champion ID 保存每支球队的所有召唤师。例如,可以在 op.gg 上找到比较值,这也是这种情况。
我目前正在使用 PHP 和 WordPress 进行编程,对于作为初级开发人员的我来说,它作为一个纯实践项目应该是有效的,而不是成为官方产品。
我希望你能帮助我。
更新
在一位同事的帮助下,我终于在他的帮助下完成了阵列。结束数组如下所示:
// Team Array.
$team_array = array();
foreach ( $data->participants as $participant ) {
$team_array[ $participant->teamId ][ $participant->participantId ]['participantId'] = $participant->participantId;
$team_array[ $participant->teamId ][ $participant->participantId ]['champion_id'] = $participant->championId;
$team_array[ $participant->teamId ][ $participant->participantId ]['summonerName'] = '';
}
// Get teamId per participant.
foreach ( $data->participantIdentities as $participantIdentity ) {
foreach ( $team_array as $teamId => $team_participants ) {
foreach ( $team_participants as $team_participantId => $team_participant ) {
if ( $team_participant['participantId'] == $participantIdentity->participantId ) {
$match_array['teams'][ $teamId ][ $team_participant['participantId'] ]['summonerName'] = $participantIdentity->player->summonerName;
$match_array['teams'][ $teamId ][ $team_participant['participantId'] ]['champion_id'] = $team_participant['champion_id'];
break;
}
}
}
}
$match_list[] = $match_array;