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
Related
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 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 answers here:
Get value from JSON array in PHP
(4 answers)
Closed 6 years ago.
I have a json data from API and i want to insert them to my database table. I have extract $data using json_encode function, but when i tried to access data inside $myJson with some of this code, it gives me an error result.
$data = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$myJson = json_decode($data);
foreach ($myJson as $mj){
echo $mj['math_score'];
// echo $mj->math_score; <= error
// echo $mj[0]->post->math_score; <= error
// echo $mj->post->math_score; <= error
}
The error : Invalid argument supplied for foreach().
sorry for my bad grammar, any answer will be greatly appreciated. Thanks
There is built in php function json_decode()
Try
$json = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$json = json_decode($json);
echo $json->posts{0}->post->math_score;
By default json_decode return object. If you want an array then you need to pass second argument true to json_decode.
Try it with Array
$json = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$json = json_decode($json, true);
foreach ($json['posts'] as $mj)
{
echo $mj['post']['math_score'];
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I've a multidimesional array in my PHP code ...
$wayPoints = $_POST['wayPoints'];
print_r ($wayPoints);
that returns
[["1dcb4f6575fbf5ee4ebd542d5981a588",7.67468,44.91085],["7503c3e97935960d0f7abcb6f7ad70f4",7.67614,44.90977]]
I need to get the values at index = 1 and index = 2 in the array: if I try to get the value
7.67468
using
print_r ($wayPoints[0][1]);
I obtain
Notice: Uninitialized string offset: 1
as error
Using
print_r ($wayPoints[0]);
I obtain
[
as error
Suggestions?
As definitely your $_POST['wayPoints'] is a json_encoded string, try this part of code:
// decode your json-string to array
$wayPoints = json_decode($_POST['wayPoints'], true);
// if you want to check what you have:
print_r($wayPoints)
// echo required item:
echo $wayPoints[0][1];
More about json you can find here, in Documentation, or with google.
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