通过多维数组搜索获取信息?

时间:2014-11-19 09:45:58

标签: php arrays multidimensional-array

我正在开发一个多维数组,显示来自RiotGames(英雄联盟)创建者API的信息。 使用用户名和summonerID(playerID)搜索其API的请求将返回信息 看起来像这样:

Array ( [summonerId] => 34943406 [playerStatSummaries] => 
Array ( 
[0] => Array ( [playerStatSummaryType] => AramUnranked5x5 [wins] => 273 [modifyDate] => 1416009440000 [aggregatedStats] => Array ( [totalChampionKills] => 5808 [totalTurretsKilled] => 298 [totalAssists] => 9025 ) ) 
[1] => Array ( [playerStatSummaryType] => Ascension [wins] => 2 [modifyDate] => 1415466770000 [aggregatedStats] => Array ( ) ) 
[2] => Array ( [playerStatSummaryType] => CAP5x5 [wins] => 41 [modifyDate] => 1416177610000 [aggregatedStats] => Array ( [totalChampionKills] => 562 [totalMinionKills] => 9087 [totalTurretsKilled] => 79 [totalNeutralMinionsKilled] => 2371 [totalAssists] => 475 ) ) 

然后继续,取决于玩家玩过多少游戏模式。每个游戏模式都以数字[1/2/3]开头,但每个用户都不一样,具体取决于他们玩过的游戏模式。

我的问题是如何搜索数组以找到" Cap5x5"例如,只取出那些统计数据,将它们显示在一个带有统计数据的干净的表格中。问题是,对于我的帐户,游戏模式" RankedSolo5x5"是[9],但对于朋友的帐户,它是[7]。这是一个非常模糊的问题,但有点难以解释。

3 个答案:

答案 0 :(得分:1)

假设统计摘要数组中始终只有一个项目,如CAP5x5:

function search($search_term){
    foreach ($result['playerStatSummaries'] as $statsItem){
        if($statsItem['playerStatSummaryType'] == $search_term) return $statsItem
    }
    return false;
}

$myStats = search('CAP5x5');

答案 1 :(得分:0)

$arr = get_riotgames_array(); // pseudocall 

array_filter($arr, function($elem) { // will filter by checking type
  return array_reduce($elem['playerStatSummaries'], function($memo, $el) {
    // whether type is OK, carry true
    return $memo = $memo || $el['playerStatSummaryType'] === 'CAP5x5'
  }, false)
});

希望它有所帮助。

答案 2 :(得分:0)

假设只在CAP5x5

中找到了playerStatSummaryType
<?php
//(...)some code you might have before this table...

function returnTable($array, $searchFor) {
  $content = '<table>';
  foreach($array['playerStatSummaries'] as $playerStat) {
     if($playerStat['playerStatSummaryType'] == $searchFor) {
         $content .= '<tr>';
             $content .= '<td>wins</td><td>' . $playerStat['wins'] . '</td>';
             $content .= '<td>totalChampionKills</td><td>' . $playerStat['aggregatedStats']['totalChampionKills'] . '</td>';
             $content .= '<td>totalMinionKills</td><td>' . $playerStat['aggregatedStats']['totalMinionKills'] . '</td>';
             $content .= '<td>totalTurretsKilled</td><td>' . $playerStat['aggregatedStats']['totalTurretsKilled'] . '</td>';
             $content .= '<td>totalNeutralMinionsKilled</td><td>' . $playerStat['aggregatedStats']['totalNeutralMinionsKilled'] . '</td>';
             $content .= '<td>totalAssists</td><td>' . $playerStat['aggregatedStats']['totalAssists'] . '</td>';

         $content .= '</tr>';
     } else {
         echo 'ERROR: Stats not found for this game mode. Try a different one';
         continue;
     }
  }

  $content .= '</table>';

  return $content;
}

echo returnTable($array, 'CAP5x5');
//OR...
echo returnTable($array, 'WhatEverGameYouWant');
//OR...
echo returnTable($array, $gamemode); //if $gamemode = 'CAP5x5' (for example...)

希望它有所帮助! :d