This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access an object property with a minus-sign?
I am trying to parse geo:point / geo:lat but getting nowhere fast. Here's the code I have so far
$content = get_data('http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=united+kingdom&api_key=XXX&format=json&limit=10000&festivalsonly=1');
$LFMdata = json_decode($content);
foreach ($LFMdata->events->event as $event) {
$venue_lat = $event->venue->location["geo:point"]["geo:lat"];
$venue_long = $event->venue->location["geo:point"]["geo:long"];
The JSON will contain something like
"geo:point": {
"geo:lat": "52.7352",
"geo:long": "-1.695392"
}
Anyone got any idea? Seen examples in JavaScript but not PHP
You can use
$venue_lat = $event->venue->location->{"geo:point"}->{"geo:lat"};
Or make it return everything like an associative array (second param of json_decode set to true):
$LFMdata = json_decode($content, true);
foreach ($LFMdata["events"]["event"] as $event) {
$venue_lat = $event["venue"]["location"]["geo:point"]["geo:lat"];
$venue_long = $event["venue"]["location"]["geo:point"]["geo:long"];
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I've a json response like this:
'{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}'
above code is in a variable as string
I need just product keys: 123,234;
how can I get product keys from this reponse?
Thanks
Use json_decode to turn it into an associative array.
$jsonString = '{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}';
$json = json_decode($jsonString, true);
$productKeys = [];
foreach($json["data"] as $val)
{
$productKeys[] = $val["productKey"];
}
This question already has answers here:
json_decode to array
(12 answers)
Closed 2 years ago.
I am trying to access the fields of a JSON-File. The JSON look like:
{"ip":"83.215.135.170","location:"{"country":"AT","region":"Salzburg","city":"Salzburg","lat":47.8125,"lng":13.0504,"postalCode":"5020"
I want to access the latitude and longitude of this file. My current code looks like that:
$data = json_decode($json);
echo $data["location:"]["lat"];
I get the error that the index is undefined. Can anybody help me?
You have a corupted JSON...here is the correct one
$json = '$json = '{"ip":"83.215.135.170","location":{"country":"AT","region":"Salzburg","city":"Salzburg","lat":47.8125,"lng":13.0504,"postalCode":"5020"}}';
With correct JSON you do this:
$decodedJson = json_decode($json,true);
And now you can do this:
echo $decodedJson["location"]["lat"]; // Prints 47.8125
if you want to access the fields like an array, you got to add "true" as a second parameter.
https://www.php.net/manual/de/function.json-decode.php
$data = json_decode($json, true);
echo $data["location:"]["lat"];
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
how to get value from array in php from api
here is my code
$data = callBitAPI('GET','https://min-api.cryptocompare.com/data/pricemultifull?fsyms='.strtoupper($bc->slug).'&tsyms=USD');
if(!empty($data)){
$data = json_decode($data,true);
// echo '<pre>';
// print_r($data);
$data = getRelevantCryptoArray($data,$bc->slug);
}
and here is my result
Please see Attachment
http://prntscr.com/k7hawi
Um... simply
$price = $data['RAW']['BTC']['USD']['PRICE'];
print_r($price);
unless I'm missing something?
dd($data['RAW']['BTC']['USD']['PRICE']) and see the result
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
How do I return only all the urls item "webformatURL" using json decode?
{
"totalHits":500,
"hits":[
{
"previewHeight":84,
"likes":37,
"favorites":42,
"tags":"yellow, natural, flower",
"webformatHeight":360,
"views":11218,
"webformatWidth":640,
"previewWidth":150,
"comments":8,
"downloads":6502,
"pageURL":"https://example.com/en/yellow-natural-flower-715540/",
"previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
"webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
"imageWidth":3020,
"user_id":916237,
"user":"916237",
"type":"photo",
"id":715540,
"userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
"imageHeight":1703
},
],
"total":7839
}
I try:
$test= json_decode($json);
$result= $test->webformatURL;
Error: warning-undefined-property-stdclass::webformatURL
I've read the manual but I doubt then I would see an example in practice
json_decode
Thanks for any help
Did you mean?
$result = $test->hits[0]->webformatURL;
If you want to extract only this field from the object, you can do:
$urls = [];
foreach($test->hits as $hit) {
$urls[] = $hit->webformatURL;
}
var_dupm($urls);
This question already has answers here:
Read Json/Array string in Php
(2 answers)
Closed 8 years ago.
In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:
{ "checkouts":[
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
},
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
}
]
}
i want to get the first_name record. Is it possible using php ?
this is the php code:
<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
echo $result->name;
}
?>
After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.
$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array