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.
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 months ago.
I have output file json but i can't read the properties in array [0]
contacts:
Array(20)
0:
canonical-vid:00000
form-submissions:[]
identity-profiles:[{…}]
is-contact:true
merge-audits:[]
merged-vids:[]
portal-id:000000
properties:{....}
I use this code in php :
$json = file_get_contents('./file,json');
$data = json_decode($json,true);
$firstname = $data['contacts'][0]['properties']['firstname']['value'];
I think $data['contacts'][0]['properties'] is json. Try like this:
$properties = $data['contacts'][0]['properties'];
$nameData = json_decode($properties,true);
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 8 months ago.
I have following php api to fetch some third party json data.
<?php
$url = 'https://players-facts-api.herokuapp.com/api/v1/resources/players?number=1'; // path to your JSON file
$imgUrl='https://random.cricket/players.json';
$data = file_get_contents($url); // put the contents of the file into a variable
$dataImg= file_get_contents($imgUrl);
$characters = json_decode($data); // decode the JSON feed
$imgCharacters = json_decode($dataImg, true);
echo $characters[0]->fact;
echo $imgCharacters[0]->url;
print_r($imgCharacters);
?>
Here, when I run this php file, it gives me the correct output for
echo $characters[0]->fact;
But it kept giving me an error for the
echo $imgCharacters[0]->url;
The error is,
Warning: Attempt to read property "url" on null in C:\xampp\htdocs\cricinfo\player-fact-app\index.php on line 11
When I print the second array, it giving me something like follows,
Array ( [fileSizeBytes] => 285621 [url] => https://random.players/a62ccc75-2b8b-48d1-9110-b6e8d5687c07.jpg )
You parsed the second array as an associative array. There is no index 0, you can access the url using the key $imgCharacters['url']. (without -> as it is not an object)
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
How can I iterate to get id value? This is my array:
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
tried
<?php
foreach($faculty as $value)
{
echo $value['Id'];
}
?>
Gives an error
Use of undefined constant Id - assumed Id
This is a json which is basically a string, to be more precise the given json contains a list (currently 1 element):
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
You need to convert it to an array first:
$jsonValues = json_decode($json, true); //here you will have an array of users (1 now)
foreach($jsonValues as $faculty) //for each user do something
{
echo $faculty['Id'];
}
This is JSON format. First you have to decode it. Example:
$a = '[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]';
$dec = json_decode($a);
echo $dec[0]->Id;
Result: 216
Decoded you have an array, containing exactly one object. You have to access the object properties with -> then.
With JSON [] brackets means an array, while {} brackets mean objects. Learn more: https://en.wikipedia.org/wiki/JSON
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 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.