我想从API获取数据,但我有问题:
foreach ($userdata as $Summoner)
{
$response = Unirest::get("https://community-league-of-legends.p.mashape.com/api/v1.0/$region/summoner/retrieveInProgressSpectatorGameInfo/" .
$Summoner['NICK'], array("X-Mashape-Authorization" =>
"$key"), null);
$array = ($response->raw_body);
$new = json_decode($array, true);
}var_dump ($new);
array(5) { ["playerCredentials"]=> array(11) { ["observerEncryptionKey"]=> string(32) "nZvmi1uhrXGW4hn1bD9u9eadte7x9dGK" ["dataVersion"]=> int(0) ["playerId"]=> int(28980822) ["serverPort"]=> int(0) ["observer"]=> bool(true) ["summonerId"]=> int(0) ["championId"]=> int(0) ["observerServerIp"]=> string(12) "95.172.65.26" ["gameId"]=> int(859406155) ["observerServerPort"]=> int(8088) ["lastSelectedSkinIndex"]=> int(0) } ["dataVersion"]=> int(0) ["gameName"]=> string(15) "match-859406155" ["reconnectDelay"]=> int(0) ["game"]=> array(33) { ["practiceGameRewardsDisabledReasons"]=> array(1) { ["array"]=> array(0) { } } ["glmSecurePort"]=> int(0) ["queuePosition"]=> int(0) ["playerChampionSelections"]=> array(1) { ["array"]=> array(10) .............但是当我循环$new
foreach ($new as $usrGmID) {
}我明白这一点: var_dump ($usrGmID);
array(33) { ["practiceGameRewardsDisabledReasons"]=> array(1) { ["array"]=> array(0) { } } ["glmSecurePort"]=> int(0) ["queuePosition"]=> int(0) ["playerChampionSelections"]=> array(1) { ["array"]=> array(10) { [0]=> array(6) { ["spell1Id"]=> int(14) ["spell2Id"]=> int(4) ["championId"]=> int(238) ["summonerInternalName"]=> string(4) "nali" ["selectedSkinIndex"]=> int(0) ["dataVersion"]=> int(0) } [1]=> array(6) { ["spell1Id"]=> int(4) ["spell2Id"]=> int(3) ["championId"]=> int(99) ["summonerInternalName"]=> string(9) "gamepl121".......... 所有我不想得到的数据都消失了。
那么,我的问题是如何获得"playerId“和"gameId”,并将其推入新数组?
我试着用这种方式:
$i = 0;
foreach ($userdata as $Summoner)
{
$response = Unirest::get("https://community-league-of-legends.p.mashape.com/api/v1.0/$region/summoner/retrieveInProgressSpectatorGameInfo/" .
$Summoner['NICK'], array("X-Mashape-Authorization" =>
"$key"), null);
$array = ($response->raw_body);
$new[$i] = json_decode($array, true);
$i = $i + 1;
}
foreach ($new as $usrGmID)
{
array_push($sumgameid, array("playerId" => $usrGmID["playerCredentials"]["playerId"],
"gameId" => $usrGmID["playerCredentials"]["gameId"]));
}但这毁了我下面所有的代码行。
还有别的办法吗?
发布于 2014-06-09 21:16:50
看起来您访问数组的方式不正确。
尝试:
$new = array(
0 => array(
'playerCredentials' => array(
'observerEncryptionKey' => '',
'dataVersion' => '',
'playerId' => 'qwe',
'serverPort' => '',
'observer' => '',
'gameId' => '3425'
)
)
);
$newArray = array();
foreach ($new as $userGmID)
{
$newArray[] = array('
playerId' => $userGmID['playerCredentials']['playerId'],
'gameId' => $userGmID['playerCredentials']['gameId']
);
}
echo '<pre>', print_r($newArray, true), '</pre>';
this outputs the values of the 2 keys you are looking for. Since you have a root array that have 0 based indices, this will look and access the child keys with your data.最新情况:
在你原来做json调用的地方
foreach ($userdata as $Summoner)
{
$response = Unirest::get("https://community-league-of-legends.p.mashape.com/api/v1.0/$region/summoner/retrieveInProgressSpectatorGameInfo/" .
$Summoner['NICK'], array("X-Mashape-Authorization" =>
"$key"), null);
$array = ($response->raw_body);
$new[] = json_decode($array, true);
}通过使用[],您已经将一个新的数组索引“推入”到数组中。不需要指定索引值。我想也许你的数组是用时髦的数组索引构建的。
同时,为了避免在哪里看到的错误,请执行以下操作
foreach ($new as $userGmID)
{
if (isset($userGmID['playerCredentials']))
{
$newArray[] = array('
playerId' => $userGmID['playerCredentials']['playerId'],
'gameId' => $userGmID['playerCredentials']['gameId']
);
}
}https://stackoverflow.com/questions/24129093
复制相似问题