I need to read firebase JSON URL in php and then display it.
My firebase has got below .json data:
{"dDsdE4AlB7P5YYd4fWbYTQKCLPh1":{"email":"abhi#gmail.com","name":"abhishek"},
"z1ceiLhdh9YVu7lGnVvqDWoWHFH3":{"email":"ravi#gmail.com","name":"ravish"},
"ghg76JHG8jhkj7GHGJ763kjhFF45":{"email":"John#gmail.com","name":"John"}}
I am able to access "name" field using the key value, as below:-
$url = 'https://xxxx.firebaseio.com/users.json'; // path to JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed
echo $characters->dDsdE4AlB7P5YYd4fWbYTQKCLPh1->name;
Now, how to access "name" field without knowing the key value? Because these keys are automatically generated by firebase and could be any value.
Many thanks in advance!
Make an associative array from your json. Then you can traverse it using foreach with keys and values. That way you will have the key value also the associated data to the key.
$characters = json_decode($data, 1);
foreach ($characters as $key => $value) {
echo $key . ' ' . $echo $value['name'];
}
You could also try to get the keys themselves:
array_keys($yourEncodedJson);
This will return all key names in the provided array.
This way, you won't need to loop through them.
Related
I have a problem statement where I need to find value of a key where I might have to escape few words while searching.
Example :
{"The length is {value} " : "done"}
I should get the output "done" if I search for The length is 10, or The length is 20. I am trying to get this done in php.
First turn your json into an array with json_decode($json, true)
You can create a variable like $key = "The length is" . $number;
And then use key_exist function to check if the $Key exist in your array.
If it exist then you can get the value by key name from your array
decode your json using json_decode() function
get key json array using array_keys() function
get first key pass in json array and get your value
Like this:
$json_array = json_decode($json, true);
$key_array = array_keys($json_array);
echo $json_array[$key_array[0]];
Im trying to iterate over the file https://alexa.rob-balmbra.co.uk/tracker/mapping.json
Im trying to loop over each name as in "4-72", however i want access to its contents and its name.
Im doing a search in its content and when I have matched the code, I need to subsequently get the key as in '4-72', etc...
Every time I use foreach i get the contents of the json file rather than the key and the contents.
//Fetch the JSON file and decode it into an associative array
$json = json_decode( file_get_contents('https://alexa.rob-balmbra.co.uk/tracker/mapping.json'), true );
//This is the code you want to search for
$searchCode = "03013";
//Loop through the json array, get key and data
foreach($json as $key=>$data){
//If the code matches what you're searching for, echo the key out and move past the foreach loop.
if($data["code"] == $searchCode){
echo "The service name is: " . $key;
break;
}
}
//You don't need this here. I just used it so I could see all the codes.
echo '<pre>' . print_r($json, 1) . '</pre>';
The code explained:
Go and get the JSON file from the web page
Decode the JSON file into an associative array so PHP can work on it
Set up a search code
For all the keys, search each code to see if it matches the search code
If there is a match, Show the respective key.
Every time I use foreach i get the contents of the json file rather than the key and the contents.
Have you turned the JSON string into an array?
$jsonValues = json_decode($jsonString,true) ;
foreach($jsonValues as $key=>$data) {
// do your thing here
}
I have some issue with a decoded Json object sended to a php file. I have tried some different format like this
{"2":"{Costo=13, ID=9, Durata_m=25, Descrizione=Servizio 9}","1":"{Costo=7, ID=8, Durata_m=20, Descrizione=Servizio 8}"}
or this.
[{"Costo":"7.5","ID":"3","Durata_m":"30","Descrizione":"Barba Rasoio"},{"Costo":"4.5","ID":"4","Durata_m":"20","Descrizione":"Barba Macchinetta"}]
In order the first, any suggestions helps me, then i have converted previous string using GSON, however php doesn't decode.
This is my php:
//Receive JSON
$JSON_Android = $_POST["JSON"];
//Decode Json
$data = json_decode($JSON_Android, true);
foreach ($data['servizi'] as $key => $value)
{ echo "Key: $key; Value: $value<br />\n";
}
How can I access single elements of array? What I'm doing wrong? Thanks in advance
I think you should check the content in this way
//Receive JSON
$JSON_Android = $_POST["JSON"];
//Decode Json
$data = json_decode($JSON_Android, true);
foreach ($data as $key => $value) {
echo "FROM PHP: " . $value;
echo "Test : " .$value['ID'];
}
your elements of {Costo=7, ID=8, Durata_m=20, Descrizione=Servizio 8}
are not properly formated as an array element. That is a pure string and not an array value.
This is a json object with 1 element of array:
{
"1": {
"Costo": 7,
"ID": 8,
"Durata_m": 20
}
}
The Inside are json objects. Therefore your json string was not properly formated to operate with that logic. What you had was an element of a string. That is the reason why it was a valid json (passing jsonlint) but was not the correct one that you wanted to use.
UPDATE
Because this format is fix, I have a non-elegant way:
//Receive JSON
$JSON_Android = $_POST["JSON"];
//Decode Json
$data = json_decode($JSON_Android, true);
foreach ($data as $key => $value) {
//to separate each element
$newArray = explode(',',$value);
$newItem = explode('=', $newArray[1]);
echo "ID". $newItem[1];
}
That would be the dirty way to do it ONLY IF THE PLACEMENT OF DATA IS FIX. (ie the second element of the first explode is always ID.
I will leave it to someone else to make the suggested code better. I would recommend more to ensure that the json you are receive is proper because as I explained, it is incorrectly formated and as an api developer, you want an adaptive way for any given client to use the data efficiently.
I've have this list of products in JSON that needs to be decoded:
"[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]"
After I decode it in PHP with json_decode(), I have no idea what kind of structure the output is. I assumed that it would be an array, but after I ask for count() it says its "0". How can I loop through this data so that I get the attributes of each product on the list.
Thanks!
To convert json to an array use
json_decode($json, true);
You can use json_decode() It will convert your json into array.
e.g,
$json_array = json_decode($your_json_data); // convert to object array
$json_array = json_decode($your_json_data, true); // convert to array
Then you can loop array variable like,
foreach($json_array as $json){
echo $json['key']; // you can access your key value like this if result is array
echo $json->key; // you can access your key value like this if result is object
}
Try like following codes:
$json_string = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$array = json_decode($json_string);
foreach ($array as $value)
{
echo $value->productId; // epIJp9
echo $value->name; // Product A
}
Get Count
echo count($array); // 2
Did you check the manual ?
http://www.php.net/manual/en/function.json-decode.php
Or just find some duplicates ?
How to convert JSON string to array
Use GOOGLE.
json_decode($json, true);
Second parameter. If it is true, it will return array.
You can try the code at php fiddle online, works for me
$list = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$decoded_list = json_decode($list);
echo count($decoded_list);
print_r($decoded_list);
I'm using the AWS PHP SDK along with the code listed below to return a list of objects associated with a folder on my Amazon S3 service:
$s3 = new AmazonS3();
$response = $s3->list_objects($bucket, array(
'prefix' => 'myfolder/'
));
print_r($response->body);
I don't want to use "print_r" part.
The $response seems to be an array with a bunch of stuff in it:
Key, LastModified, ETag, Size, Owner
What would the PHP code look like that would loop through $response and assign one of the bits to a variable. For example, one piece of data in $response is "Key" that looks something like this:
[Key] => myfolder/myfile.pdf
what I need is:
myfolder/myfile.pdf
Please provide the code I would need to loop through the data within $response and assign each instance of "KEY" to a variable called: $haasfilepath.
Thanks!
It has been awhile since I've used php, but this should be work if I understand your problem.
If $response is an array with each element containing an associative array for the matching object.
$hassfilepath = Array();
foreach($response as $element){
$haasfilepath << $element[key];
}
The foreach will allow you to loop through all the objects returned in response. Within the loop you then push each [Key] into the array that is $haasfilepath. You now have a variable with an array of all key's returned.
As far as I understand, the response will be an XML file (not an array-name-value-pair) and you have to parse the XML file. Try researching xpath and similar methods in parse XMl file in php.
foreach ($response->body as $key => $haasfilepath)
{
echo 'value for key ', $key, ' is ', $haasfilepath, '<br>';
}
or, if you only need the key "key":
$haasfilepath = $response->body['key'];
echo $haasfilepath;