I want to fetch player fields:
e.g id,name,tag,plat etc
I have this 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 Five III
[needed] => 4920000
[next] => stdClass Object
(
[nr] => 74
[img] => r74
[name] => Chief Warrant Officer Five 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 I fetch this data?
Here is my 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 get the name you should do this:
echo $data->player->name;
You need this because you have a stdClass Object
You can also change the out to just a array, you have to change this:
$data = json_decode($json);
to this:
$data = json_decode($json, true);
When you make this chagne you can get the value by this:
echo $data['player']['name'];
Edit
Below you can see the example, this works for me with 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;
$result = array();
$result['name'] = $data->Player->name
var_dump($result);
Not sure what do you mean by fetch the data as already fetched it but if you ask how to access the fields you can do it like this.
print_r($data->player->name);
Related
I am getting below json data thru Shiprocket API. Now I want to extract value of below variables in PHP code from this json.
I have tried to use json_decode but it did not work and show null value:
$data = json_decode($json);
$sr_status = $data['shipment_status'];
Please suggest the code to retrieve below variables value.
shipment_status , awb_code , courier_company_id
Array
(
[0] => stdClass Object
(
[tracking_data] => stdClass Object
(
[track_status] => 1
[shipment_status] => 7
[shipment_track] => Array
(
[0] => stdClass Object
(
[id] => 180339484
[awb_code] => 11150911492
[courier_company_id] => 55
[shipment_id] => 1711169662
[order_id] => 233223781187
[pickup_date] => 2023-01-11 03:02:00
[delivered_date] => 2023-01-16 12:22:00
[weight] => 0.25
[packages] => 1
[current_status] => Delivered
[delivered_to] => Solapur
[destination] => Solapur
[consignee_name] => ABC
[origin] => Ludhiana
[courier_agent_details] =>
[edd] =>
)
)
[shipment_track_activities] => Array
(
[0] => stdClass Object
(
[date] => 2023-01-16 12:22:00
[status] => 000-T-DL
[activity] => SHIPMENT DELIVERED
[location] => SOLAPUR
[sr-status] => 7
[sr-status-label] => DELIVERED
)
[1] => stdClass Object
(
[date] => 2023-01-16 11:34:00
[status] => 002-S-UD
[activity] => SHIPMENT OUTSCAN
[location] => SOLAPUR
[sr-status] => 17
[sr-status-label] => OUT FOR DELIVERY
)
)
[track_url] => https://shiprocket.co//tracking/11150911492
[etd] => 2023-01-14 17:02:00
[qc_response] => stdClass Object
(
[qc_image] =>
[qc_failed_reason] =>
)
)
)
)
you can try this:
$array = ...; // Your array here
$data= json_decode($array);
$shipment_status = $data[0]->tracking_data->shipment_status;
$awb_code = $data[0]->tracking_data->shipment_track[0]->awb_code;
$courier_company_id = $data[0]->tracking_data->shipment_track[0]->courier_company_id;
Or use $data = json_decode($json,true); which return an array where you can use
foreach($data as $val) {
$shipment_status = $val['tracking_data']['shipment_status'];
foreach ($val['shipment_track'] as $value) {
$awb_code = $value['awb_code'];
$courier_company_id = $value['courier_company_id'];
}
}
So i was trying to get "title" and "Artist->name" from all the indexes
and i tried many times even used foreach but i am still learning curl and can only get it for the first index or a specific index here is the table
(
[data] => Array
(
[0] => Array
(
[id] => 2298089
[readable] => 1
[title] => Broken Strings
[title_short] => Broken Strings
[title_version] =>
[link] => https://www.deezer.com/track/2298089
[duration] => 250
[rank] => 749501
[explicit_lyrics] =>
[explicit_content_lyrics] => 0
[explicit_content_cover] => 0
[preview] => https://cdns-preview-1.dzcdn.net/stream/c-140e953bb38254bc8db4fe4dc97dd689-8.mp3
[md5_image] => 71698262cce4bb1c0e3d25e0707ed092
[artist] => Array
(
[id] => 4523
[name] => James Morrison
[link] => https://www.deezer.com/artist/4523
[picture] => https://api.deezer.com/artist/4523/image
[picture_small] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/56x56-000000-80-0-0.jpg
[picture_medium] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/250x250-000000-80-0-0.jpg
[picture_big] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/500x500-000000-80-0-0.jpg
[picture_xl] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/1000x1000-000000-80-0-0.jpg
[tracklist] => https://api.deezer.com/artist/4523/top?limit=50
[type] => artist
)
[album] => Array
(
[id] => 229099
[title] => Songs For You, Truths For Me (International Exclusive Bundle)
[cover] => https://api.deezer.com/album/229099/image
[cover_small] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/56x56-000000-80-0-0.jpg
[cover_medium] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/250x250-000000-80-0-0.jpg
[cover_big] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/500x500-000000-80-0-0.jpg
[cover_xl] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/1000x1000-000000-80-0-0.jpg
[md5_image] => 71698262cce4bb1c0e3d25e0707ed092
[tracklist] => https://api.deezer.com/album/229099/tracks
[type] => album
)
[type] => track
)
[1] => Array
(
[id] => 1196110412
etc.., and here is the code
$ch = curl_init();
$url = "https://api.deezer.com/search?q=broken%20strings";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
if($e = curl_error($ch)){
echo $e;
}else{
$decoded = json_decode($resp,true);
print_r($decoded);
}
curl_close($ch);
this posts all of array
$decoded['data'] will contain an array with all your records, so you want to foreach over that:
foreach ($decoded['data'] as $record) {
On each iteration of that loop, $record will be the subarray for each individual record. So on the first iteration, $record will be:
Array(
[id] => 2298089
[readable] => 1
[title] => Broken Strings
[title_short] => Broken Strings
[title_version] =>
[link] => https://www.deezer.com/track/2298089
[duration] => 250
[rank] => 749501
...
Then you can reference each field in that array like $record['id'] or $record['link']. To "drill down" into multi-dimensional arrays, just stack consecutive [] references, like $record['album']['id'] or $record['artist']['link'].
All together:
foreach ($decoded['data'] as $record) {
printf("Title: %s\n", $record['title']);
printf("Artist: %s\n\n", $record['artist']['name']);
}
I have the following problem.
I am trying to read a Json file but I am not getting the desired result. I have the following data.
I would like to read the features and there is a mistake in finding out somewhere.
I get the error message when reading out:
Warning: Illegal string offset 'features'
Output
Array
(
[objectIdFieldName] => OBJECTID
[uniqueIdField] => Array
(
[name] => OBJECTID
[isSystemMaintained] => 1
)
[globalIdFieldName] =>
[geometryProperties] => Array
(
[shapeAreaFieldName] => Shape__Area
[shapeLengthFieldName] => Shape__Length
[units] => esriMeters
)
[geometryType] => esriGeometryPolygon
[spatialReference] => Array
(
[wkid] => 4326
[latestWkid] => 4326
)
[fields] => Array
(
[0] => Array
(
[name] => GEN
[type] => esriFieldTypeString
[alias] => GEN
[sqlType] => sqlTypeOther
[length] => 33
[domain] =>
[defaultValue] =>
)
[1] => Array
(
[name] => cases
[type] => esriFieldTypeInteger
[alias] => Anzahl Fälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
[2] => Array
(
[name] => deaths
[type] => esriFieldTypeInteger
[alias] => Anzahl Todesfälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
)
[features] => Array
(
[0] => Array
(
[attributes] => Array
(
[GEN] => Celle
[cases] => 220
[deaths] => 15
)
...........
My Code
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = json_decode($json,true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
foreach($data as $row){
echo $row['features']->$row['attributes']->$row['GEN'];
}
Where am I wrong in reading?
It only has to be read what is in parentheses right?
So actually features-> attributes-> Gen to get the GEN query
Let's try change to this.
foreach($data as $row){
echo $row['features'][0]['attributes']['GEN'];
}
Sorry this is my fault when seem does'nt read clearly your question.
With GEN attribute you don't use loop to get this value.
If you wanna get GEN only your code should be that.
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = (array) json_decode($json, true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
echo $data['features']['attributes']['GEN'];
// foreach($data as $row){
// echo $row['features']->$row['attributes']->$row['GEN'];
// }
I've that xml structure retrieving from device
<packet>
<info action="fiscalmemory" fiscalmemorysize="1048576" recordsize="464" fiscal="1" uniqueno="ABC12345678" nip="123-456-78-90" maxrecordscount="2144" recordscount="7" maxreportscount="1830" reportscount="4" resetmaxcount="200" resetcount="0" taxratesprglimit="30" taxratesprg="1" currencychangeprglimit="4" currencychangeprg="0" fiscalstartdate="dd-mm-yyyy hh:dd:ss" fiscalstopdate="dd-mm-yyyy hh:dd:ss" currencyname="PLN" />
<ptu name="A" bres="Nobi">123.23</ptu>
<ptu name="B">123.23</ptu>
<ptu name="D">8</ptu>
<sale>999.23</sale>
</packet>
simpleXml does't see ptu's attributes
$array = simplexml_load_string($xml);
print_r($array);
It prints
SimpleXMLElement Object
(
[info] => SimpleXMLElement Object
(
[#attributes] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[ptu] => Array
(
[0] => 123.23
[1] => 123.23
[2] => 8
)
[sale] => 999.23
)
As we can see ptu doesn't contain attributes :/
I also tried parse it with recursive function because children also may contain chilren but without success :/
Could anybody point to me why SimpleXML doesn't take ptu's attributes and also share any parsing function?
Thanks in advance.
edited
As regards Nigel Ren I made that function
function parseXMLtoArray($xml){
$x = simplexml_load_string($xml);
$result = [];
function parse($xml, &$res){
$res['name'] = $xml->getName();
$res['value'] = $xml->__toString();
foreach ($xml->attributes() as $k => $v){
$res['attr'][$k] = $v->__toString();
}
foreach($xml->children() as $child){
parse($child, $res['children'][]);
}
}
parse($x, $result);
return $result;
}
$resArray = parseXMLtoArray($rawXml);
print_r($resArray);
this returns such array
Array
(
[name] => packet
[value] =>
[attr] => Array
(
[crc] => BKJFKHKD54
)
[children] => Array
(
[0] => Array
(
[name] => info
[value] =>
[attr] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[1] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => B
)
)
[3] => Array
(
[name] => ptu
[value] => 8
[attr] => Array
(
[name] => D
)
)
[4] => Array
(
[name] => sale
[value] => 999.23
)
)
)
Is this correct?
Thanks again Nigel
When loading with SimpleXML, using print_r() gives only an idea of the content and doesn't have the full content. If you want to see the full content then use ->asXML()...
$array = simplexml_load_string($xml);
echo $array->asXML();
To check the attributes of <ptu> element, (this just gives the first one)...
echo $array->ptu[0]['name'];
This uses ->ptu to get the <ptu> element and takes the first one [0] and then takes the name attribute using ['name'].
Here is my json response:
stdClass Object ( [is_claimed] => [rating] => 4 [mobile_url] => http://m.yelp.com/biz/the-waterboy-sacramento [rating_img_url] => http://s3-media4.ak.yelpcdn.com/assets/2/www/img/c2f3dd9799a5/ico/stars/v1/stars_4.png [review_count] => 455 [name] => The Waterboy [snippet_image_url] => http://s3-media1.ak.yelpcdn.com/photo/SZyFYvQmHWSLhK96SSzwwA/ms.jpg [rating_img_url_small] => http://s3-media4.ak.yelpcdn.com/assets/2/www/img/f62a5be2f902/ico/stars/v1/stars_small_4.png [url] => http://www.yelp.com/biz/the-waterboy-sacramento [menu_date_updated] => 1387494198 [reviews] => Array ( [0] => stdClass Object ( [rating] => 5 [excerpt] => AMAZING again, went here last Thursday at 5:00pm. Greeted by friendly man, he asked if we had a reservation, I said, 'no, do we need one?' He said, I'm... [time_created] => 1395158338 [rating_image_url] => http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png [rating_image_small_url] => http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png [user] => stdClass Object ( [image_url] => http://s3-media1.ak.yelpcdn.com/photo/SZyFYvQmHWSLhK96SSzwwA/ms.jpg [id] => H0qUqWctz5Ms6qdeaIvjFw [name] => Lori T. ) [rating_image_large_url] => http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png [id] => bZznirkpADmW3Qcpc5u_VA ) ) [phone] => 9164989891 [snippet_text] => AMAZING again, went here last Thursday at 5:00pm. Greeted by friendly man, he asked if we had a reservation, I said, 'no, do we need one?' He said, I'm... [image_url] => http://s3-media1.ak.yelpcdn.com/bphoto/9e5sodvvP3p6_53wOqVTcg/ms.jpg [categories] => Array ( [0] => Array ( [0] => French [1] => french ) [1] => Array ( [0] => Italian [1] => italian ) ) [display_phone] => +1-916-498-9891 [rating_img_url_large] => http://s3-media2.ak.yelpcdn.com/assets/2/www/img/ccf2b76faa2c/ico/stars/v1/stars_large_4.png [menu_provider] => single_platform [id] => the-waterboy-sacramento [is_closed] => [location] => stdClass Object ( [city] => Sacramento [display_address] => Array ( [0] => 2000 Capitol Ave [1] => Midtown [2] => Sacramento, CA 95811 ) [neighborhoods] => Array ( [0] => Midtown ) [postal_code] => 95811 [country_code] => US [address] => Array ( [0] => 2000 Capitol Ave ) [state_code] => CA ) )
which I get using:
$response = json_decode($data);
print_r($response);
echo $response["rating"]; //Why this does not give json response value?
Pass true into the second parameter of json_decode to return an array (which is how you're trying to access it):
$response = json_decode($data, true); // associative array returned
print_r($response);
echo $response["rating"]; // 4
Manual
Otherwise, access the rating as a property of the object that you've got:
$response = json_decode($data);
echo $response->rating; // 4