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)
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 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 create a JSON object
(5 answers)
Closed 3 years ago.
I have the following PHP code, which gets executed when I hit the Submit button in a form:
// Save data
if(isset($_POST['save'])){
$newDataArr = array();
foreach ($data_array[0] as $k=>$v){
$newDataArr[] = array($k => $_POST[$k]);
}// ./ foreach
echo json_encode($newDataArr);
}
The echo I get is the following:
[{"ID->id":"esKZSCDfIC"},{"DT->createdAt":"2020-01-26T13:02:42"},{"DT->updatedAt":"2020-01-26T13:02:42"},{"ST->aString":"hey1"},{"NU->number":123},{"GPS->coords":["2.2222","44.4444"]},{"BL->aBool":false},{"AR->theArray":["xx","ww"]},{"FL->theFile":"https:\/\/xscoder.com\/xserver\/uploads\/6dydDtoTt5JjZzFc5L5V_image.jpg"},{"PO->userPointer":"vbN3b0C7bC"}]
Then I'll have to add that array into my JSON file as it follows:
$data = json_encode(array_values($newDataArr), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents('Test.json', $data);
But of course the syntax of my $newDataArr is not the right one, each JSON object must not be inside the { }, so this:
{"ID->id":"esKZSCDfIC"},
must become:
"ID->id":"esKZSCDfIC",
Is there a way to dynamically create a valid JSON array and push it to my Test.json file?
I've been through many posts on StackOverflow, but the result I get is always the same. I must use the PHP code above to get keys and values from my HTML inputs.
Instead of creating and pushing a new array on each iteration, set the values this way:
$newDataArr[$k] = $_POST[$k];
Hope this helps.
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:
Append data to JSON array using PHP
(3 answers)
Closed 4 months ago.
I am trying to call array_push to add data sent via a GET request to my json array that holds registration ids for my GCM Clients
<?php
//read current regids
$myfile = fopen("regids.json", "r") or die("Unable to open file!");
$current_regids = fread($myfile,filesize("regids.json"));
// decode json
$decoded_json= json_decode($current_regids);
//save to php format array
$array = array($decoded_json);
//close file
fclose($myfile);
//get registration id
$regid = $_GET["regid"];
//push new reg id into array
array_push($array,$regid);
echo json_encode($array);
?>
The JSON should be as follows
["regid1","regid2", "regid3","regid4"]
However when I run the code it inorder to array_push "regid5" it gives me this
[["regid1","regid2","regid3","regid4"],"regid5"]
Its a major headache
You already get an array when you decode it:
// decode json
$decoded_json= json_decode($current_regids);
// now you have an array or object, depending on the input
// in your case it seems to be an array
And then you put the result in another array:
//save to php format array
$array = array($decoded_json);
So now you have a nested array.
You need to remove this line / use $decoded_json as the array you want to manipulate:
$array = array($decoded_json);
Note:- If you use array_push() to add one element to the array it's better to use $array[] = "value" because in that way there is no overhead of calling a function and it is also much faster and safer than array_push().