从查询循环数据
Loop data from query
您好,我需要从 'player_list'
table 的查询中加载数据,但现在我不知道如何...请帮助
JSON
{"status":"Online",
"hostname":"[FUNPLAY.pro] AWP Ultimate | Sniper War [RAFFLES]",
"players":"6",
"slots":"24",
"map":"awp_arcade_kp",
"memory":"1192927166",
"cpu":"13",
"players_list":[{
"name":"",
"score":0,
"time":40529.5703125,
"kills":null,
"deaths":null,
"ping":null
},{
"name":"AYAYAY",
"score":24,
"time":1069.7547607422,
"kills":null,
"deaths":null,
"ping":null
},{
"name":"Fresh","score":50,"time":1069.5672607422,"kills":null,"deaths":null,"ping":null},{"name":"skitels022","score":9,"time":892.56237792969,"kills":null,"deaths":null,"ping":null},{"name":"OsPa","score":22,"time":751.53881835938,"kills":null,"deaths":null,"ping":null},{"name":"xXPalacinkaXx","score":8,"time":612.87487792969,"kills":null,"deaths":null,"ping":null}]}
PHP代码
$server = Json_Decode(File_Get_Contents("http://query.fakaheda.eu/217.11.249.84:27408.feed"));
foreach($server['players_list'] as $player) {
echo '<span class="ipsGrid_span4">'.$player->name.'</span>';
echo '<span class="ipsGrid_span4">'.$player->score.'</span>';
echo '<span class="ipsGrid_span4">'.$player->time.'</span>';
}
它抛出这个错误
Error: Cannot use object of type stdClass as array (0)
您的 json_decode()
有第二个参数确定 return 类型。默认情况下,它会将 JSON 字符串解析为 stdObject
,而您使用错误的索引访问它
$server = Json_Decode(File_Get_Contents("http://query.fakaheda.eu/217.11.249.84:27408.feed"));
foreach($server->players_list as $player) {
echo '<span class="ipsGrid_span4">'.$player->name.'</span>';
echo '<span class="ipsGrid_span4">'.$player->score.'</span>';
echo '<span class="ipsGrid_span4">'.$player->time.'</span>';
}
要将 json 字符串解析为数组,请使用 json_encode($jsonString, true)
$server = Json_Decode(File_Get_Contents("http://query.fakaheda.eu/217.11.249.84:27408.feed"), true);
foreach($server['players_list'] as $player) {
echo '<span class="ipsGrid_span4">'.$player['name'].'</span>';
echo '<span class="ipsGrid_span4">'.$player['score'].'</span>';
echo '<span class="ipsGrid_span4">'.$player['time'].'</span>';
}
您好,我需要从 'player_list'
table 的查询中加载数据,但现在我不知道如何...请帮助
JSON
{"status":"Online",
"hostname":"[FUNPLAY.pro] AWP Ultimate | Sniper War [RAFFLES]",
"players":"6",
"slots":"24",
"map":"awp_arcade_kp",
"memory":"1192927166",
"cpu":"13",
"players_list":[{
"name":"",
"score":0,
"time":40529.5703125,
"kills":null,
"deaths":null,
"ping":null
},{
"name":"AYAYAY",
"score":24,
"time":1069.7547607422,
"kills":null,
"deaths":null,
"ping":null
},{
"name":"Fresh","score":50,"time":1069.5672607422,"kills":null,"deaths":null,"ping":null},{"name":"skitels022","score":9,"time":892.56237792969,"kills":null,"deaths":null,"ping":null},{"name":"OsPa","score":22,"time":751.53881835938,"kills":null,"deaths":null,"ping":null},{"name":"xXPalacinkaXx","score":8,"time":612.87487792969,"kills":null,"deaths":null,"ping":null}]}
PHP代码
$server = Json_Decode(File_Get_Contents("http://query.fakaheda.eu/217.11.249.84:27408.feed"));
foreach($server['players_list'] as $player) {
echo '<span class="ipsGrid_span4">'.$player->name.'</span>';
echo '<span class="ipsGrid_span4">'.$player->score.'</span>';
echo '<span class="ipsGrid_span4">'.$player->time.'</span>';
}
它抛出这个错误
Error: Cannot use object of type stdClass as array (0)
您的 json_decode()
有第二个参数确定 return 类型。默认情况下,它会将 JSON 字符串解析为 stdObject
,而您使用错误的索引访问它
$server = Json_Decode(File_Get_Contents("http://query.fakaheda.eu/217.11.249.84:27408.feed"));
foreach($server->players_list as $player) {
echo '<span class="ipsGrid_span4">'.$player->name.'</span>';
echo '<span class="ipsGrid_span4">'.$player->score.'</span>';
echo '<span class="ipsGrid_span4">'.$player->time.'</span>';
}
要将 json 字符串解析为数组,请使用 json_encode($jsonString, true)
$server = Json_Decode(File_Get_Contents("http://query.fakaheda.eu/217.11.249.84:27408.feed"), true);
foreach($server['players_list'] as $player) {
echo '<span class="ipsGrid_span4">'.$player['name'].'</span>';
echo '<span class="ipsGrid_span4">'.$player['score'].'</span>';
echo '<span class="ipsGrid_span4">'.$player['time'].'</span>';
}