循环查询数据

时间:2019-03-04 00:08:23

标签: php json feed

您好,我需要从'player_list'表中的此查询中加载数据,但是现在我不知道该怎么办。.请帮助

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>'; 
}

它抛出此错误

  

错误:无法将stdClass类型的对象用作数组(0)

1 个答案:

答案 0 :(得分:0)

您的json_decode()具有确定返回类型的第二个参数。默认情况下,当您使用错误的索引访问它时,它将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>'; 
}