php - How to fetch specific fields from array using file_get_contents -
i want fetch player fields:
e.g id,name,tag,plat etc
i have type of data:
stdclass object ( [player] => stdclass object ( [id] => 179203896 [game] => bf4 [plat] => pc [name] => helltime [tag] => dk [datecheck] => 1391437377733 [dateupdate] => 1391437377733 [datecreate] => 1386696304438 [lastday] => 20140117 [country] => [countryname] => [rank] => stdclass object ( [nr] => 73 [imglarge] => bf4/ranks/r73.png [img] => r73 [name] => chief warrant officer 5 iii [needed] => 4920000 [next] => stdclass object ( [nr] => 74 [img] => r74 [name] => chief warrant officer 5 iv [needed] => 5030000 [curr] => 5022060 [relneeded] => 110000 [relcurr] => 102060 [relprog] => 92.781818181818 ) ) [score] => 5025100 [timeplayed] => 862027 [uid] => 2832659368608119092 [uname] => helltime [ugava] => 0b8b00021ebfb32414e5a6051c2c9a40 [udcreate] => 1328606863000 [blplayer] => http://battlelog.battlefield.com/bf4/soldier/helltime/stats/179203896/pc/ [bluser] => http://battlelog.battlefield.com/bf4/user/helltime/ [editable] => [viewable] => 1 [adminable] => [linked] => ) [stats] => stdclass object ( [reset] => stdclass object ( [lastreset] => 0 [score] => 0 [timeplayed] => 0 [timeplayedsincelastreset] => 0 [kills] => 0 [deaths] => 0 [shotsfired] => 0 [shotshit] => 0 [numlosses] => 0 [numwins] => 0 )
how can fetch data?
here code:
<?php $url = "http://api.bf4stats.com/api/playerinfo?plat=pc&name=helltime"; $json = file_get_contents($url); $data = json_decode($json); echo "<pre>"; print_r($data); ?>
to name should this:
echo $data->player->name;
you need because have stdclass object
you can change out array, have change this:
$data = json_decode($json);
to this:
$data = json_decode($json, true);
when make chagne can value this:
echo $data['player']['name'];
edit
below can see example, works me no error.
$url = "http://api.bf4stats.com/api/playerinfo?plat=pc&name=helltime"; $json = file_get_contents($url); echo 'using stdclass object option<br>'; $data = json_decode($json); echo $data->player->name; echo '<br><br>using array option<br>'; $data = json_decode($json, true); echo $data['player']['name']; exit;
Comments
Post a Comment