This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 8 years ago.
I would like to know, how take the value "gamertag" from this Json (http://360api.chary.us/?gamertag=superdeder) with PHP.
i would print only the value "superdeder".
thanks a lot.
Andrea.
Use file_get_contents() and convert your JSON to an object with json_decode():
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$gamer = json_decode($json);
echo $gamer->Gamertag; // SuperDeder
Or, an array, by passing a truthy value as json_decode()s second parameter:
$gamer = json_decode($json, true);
echo $gamer['Gamertag']; // SuperDeder
Related
This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 6 years ago.
JSON:
{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
PHP:
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
var_dump(json_decode($response));
echo $response->location[0]->name;
API Call: http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Tirana
Try like this.You get the contents in json format.Use json_decode() with second parameter true to convert into array.
<?php
$json = '{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
';
$array = json_decode($json,true);
//print_r($array);
$location = $array['location'];
echo $location['name'];
?>
use json_decode to parse the json string to array, then access it with the index.
the demo
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
$array = json_decode($response, true);
echo $array['location']['name'];
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
How do I decode this json data using php to get individual values.
{"Alpha":["A","B","C","D","E","F"],"Beta":["1","2","3"],"Gamma": ["a","b","c"]}
php has a function called json_decode
$json = '{"Alpha":["A","B","C","D","E","F"],"Beta":["1","2","3"],"Gamma": ["a","b","c"]}';
$json_array = json_decode($json,TRUE); // personally I prefer arrays which the TRUE adds
$alpha = $json_array['Alpha'];
$first_alpha_value = $alpha[0];
printf("A == %s\n",$first_alpha_value);
This question already has answers here:
json_decode to array
(12 answers)
Closed 6 years ago.
I have a PHP variable that when I echo it will display an array that looks like this:
My Variable:
echo $mayVariable;
displays:
{"data":[{"id":"4756756575","name":"David","url":"https:\/\/www.somesite.com"}],"page":false}
I need to get the value from id within that array.
So I tried this:
echo $mayVariable[0]['id'];
But this doesn't give me anything.
I also tried:
echo $mayVariable['data']['id'];
and still I don't get anything in the echo...
Could someone please advise on this issue?
This JSON is an object array after decode it generally.
$json = '{"data":[{"id":"4756756575","name":"David","url":"https:\/\/www.somesite.com"}],"page":false}';
$arr = json_decode($json);
echo $arr->data[0]->id;//4756756575
If you use true as second parameter then:
$arr = json_decode($json, true);
echo $arr['data'][0]['id'];//4756756575
This question already has answers here:
How to create an array for JSON using PHP?
(8 answers)
Closed 8 years ago.
I want to make json as followsin php:
{["key1":"value1"],["key2":"value2"]}
Can any one help?
See the PHP manual for json_encode() and json_decode():
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
You can do it like this: set it to true, if you want an associative array.
$array = json_decode('[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]',true);
if you want to iterate:
foreach($array as $item) { //foreach element in $arr
$uses = $item['var1']; //etc
}
This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 8 years ago.
When I try to get data from an API using
file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
I got the result as a string. Is there any way to convert it back to array?
The string which I got is
string(202) "{"query":{"count":1,"created":"2014-03-11T13:00:31Z","lang":"en-US","results":{"rate":{"id":"USDINR","Name":"USD to INR","Rate":"60.99","Date":"3/11/2014","Time":"9:00am","Ask":"61.00","Bid":"60.98"}}}}"{"error":"","msg":""}
please help me....
In your request, you ask that the returned format be JSON-encoded (via the format=json parameter), so you can just decode the response into an array using json_decode:
$response = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$response = json_decode($response, true);
// Parse the array as you would any other
You have a JSON answer here so jo need to dekode that.
<?php
$s = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");$data =
$data = json_decode($s,true);
?>
and the $data variable will be an array.