首页 > 解决方案 > 使用 cURL 获得 While 循环?

问题描述

我目前正在做一个新项目,只是我被困在我无法找到一种方法来放置包含我想要显示的信息的玩家列表的部分......

我目前有这个:

JSON数据:

participants    
0   
teamId  100
championId  18
summonerName    "BlackedByLucian"
summonerId  23687629
1   {…}
2   {…}
3   {…}
4   {…}
5   {…}
6   {…}
7   {…}
8   {…}
9   {…}

PHP

//GET CURRENT GAME DATA
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$get_game=curl_exec($ch);
curl_close($ch);
$get_game = json_decode($get_game, true);

//PLAYER DATA
$PlayerName = $get_game['participants'][0]['summonerName'];
$PlayerTeam = $get_game['participants'][0]['teamId'];
$PlayerChampion = $get_game['participants'][0]['championId'];

我想这样展示它:

Player1  | 1 | 23

Player2  | 1 | 27

Player3  | 2 | 25

Player4  | 2 | 24

Player5  | 2 | 11

etc.

顺便提一句。0 in$get_game variable表示 json 的玩家 ID。

标签: phpcurl

解决方案


你可以按照这个思路做一些事情,利用一个for循环。你也可以使用foreach循环,但我是老派呵呵。

for($i = 0; $i < count($get_game["participants"]); $i++)
    echo $get_game["participants"][$i]["summonerName"] . " | " . $get_game["participants"][$i]["teamId"] . " | " . $get_game["participants"][$i]["championId"];

推荐阅读