How get in PHP value from array JSON response of influxdb - php

I apologize in advance if there was already a similar question. I tried to find a solution but unfortunately I still can't.
I have a JSON response from influxdb
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
and I need to print out the name of columns time and count and values to be able generate a new JSON for Google chart.
What am I able to do now is only get a statement_id=0.
$decode = json_decode($output2,true);
foreach($decode['results'] as $val){
//Actual statement_id is 0
echo "statement_id is ";
echo $val['statement_id'];
echo "<br/>";
}
But I need to print:
time, count<br/>
2020-07-02T00:00:00Z,1<br/>
2020-07-03T00:00:00Z,0<br/>
2020-07-04T00:00:00Z,0<br/>
2020-07-05T00:00:00Z,0<br/>
2020-07-06T00:00:00Z,1<br/>
or put it into the variable to be able to work with it. I already tried without converting into array just with
$decode = json_decode($output2); //without ",true"
Can you please help me how to solve it.
I've spent almost the whole day looking for a solution and I can't find a way to solve the problem.

If you need to print it out on html, just change the \n to <br>
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
echo $decode["results"][0]["series"][0]["columns"][0].",".$decode["results"][0]["series"][0]["columns"][1]."\n";
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
echo $el[0].",".$el[1];
echo "\n";
}
?>
If i understand you write this would be the code to output the JSON for the Google Chart:
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
$googlechart = array();
$googlechart[] = array($decode["results"][0]["series"][0]["columns"][0],$decode["results"][0]["series"][0]["columns"][1]);
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
$googlechart[] = array($el[0],$el[1]);
}
echo json_encode($googlechart);
?>

Related

I want to show all result from an api

I'm working with google places API , and i showed content from the api but one by one like this
<?php
$str = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.075971700000004,-0.7651981999999999&radius=5000&type=restaurant&keyword=cruise&key=AIzaSyDxXV4Ka8yiDq1-UKzlzX-MUC8csfdN8y4');
$maps_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.075971700000004,-0.7651981999999999&radius=5000&type=restaurant&keyword=cruise&key=AIzaSyDxXV4Ka8yiDq1-UKzlzX-MUC8csfdN8y4';
$maps_json = file_get_contents($maps_url);
$maps_array = json_decode($maps_json, true);
$lat = $maps_array['results'][1]['name'];
$lat2 = $maps_array['results'][2]['name'];
echo $lat;
echo "<br>";
echo $lat2;
?>
but i want to show all the result one time with a loop
Try
foreach ($maps_array['results'] as $map) {
echo $map['name'];
echo "<br>";
}
Put the results array in a foreach and make a loop from that. in that way u will get all the data instead of taking it 1 by 1. And for the numbers array u can add a ++ statement(not needed).
hope this helps. Good luck

php request to display datas from a webpage

I'm a teacher in a primary school et we're part of the weatherstation project with raspberry pi
we have a website with our measurements : http://meteovictorhugo.ddns.net:1800/demo
everything is working.
Now, we'd like to have measurements from other stations in the world tu study them and work in sciences, geography...
We can have them here : https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getalllastmeasurement
but it's not really readable for pupils
First, we would like to extract datas to put them in a table and in a second time be able to choose which station we want to display
It's a bit difficult for us
All I succeed to do is this :
$json = file_get_contents('https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getalllastmeasurement');
$json = utf8_encode($json);
$obj = var_dump(json_decode($json));
echo $obj;
which gives us, meteovictorhugo.ddns.net:1800/demo/world.php so as to say nothing ...
If someone has an idea, we will be very grateful and you'll help pupils (and their teacher) to learn things
I also try with this
$json = file_get_contents('https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getalllastmeasurement');
$json = utf8_encode($json);
$obj = var_dump(json_decode($json));
foreach ($obj->items as $value) {
$ground_temp = isset($value->humidity) ? $value->humidity : 'N/A';
echo $value->weather_stn_name . ' : ' . $ground_temp . '<br/>';
}
?>
which gives this : meteovictorhugo.ddns.net:1800/demo/world4.php
Thank you
jerome
You don't need to utf8_encode the string:
<?php
$json = file_get_contents('https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getalllastmeasurement');
$decodedData = json_decode($json, true);
print_r($decodedData); // prints all data as array
print_r($decodedData['ground_temp']); //access ground_temp temperature key

How do I edit created_at from twitter json API

I have successfully fetched data from twitter API in the form of JSON. But the time provided by twitter is still not in the dateformat I need, so I think I have to edit it first. The problem is I don't know how to edit a JSON file with PHP.
Now if I want to edit the 'created_at', how do I do it? So far, here's what I got.
$results = search($query);
header('Content-Type: application/json');
$results= json_encode($results, JSON_PRETTY_PRINT);
echo $results;
file_put_contents('json_result.json', $results);
$contents = file_get_contents('json_result.json');
$contentsDecoded = json_decode($contents, true);
echo $contentsDecoded['statuses']->created_at;
foreach ($contentsDecoded['statuses'] as $tweet) {
$date = new DateTime($tweet->created_at);
$formatted_date = $date->format("d-m-Y");
echo "Date ".$formatted_date;
}
I still get error saying that it's a property of non-object...
Edit it before putting it in the file instead of putting it, getting it again, saving again.
I think the problem lies in one of these lines:
echo $contentsDecoded['statuses']->created_at;
foreach ($contentsDecoded['statuses'] as $tweet) {
Either $contentsDecoded['statuses'] has property created_at or is an array
So accessing property created_at of an array is not valid. Try to remove that line

JSON array unset

First of all, I really don't know if that kind of topic exist. But I searched a lot and now I am here.
My question about parsing. For example I would like to unset some items.
$now = array();
$now[0]['name'] = "Hello1";
$now[0]['si'] = "BumBum1";
$now[1]['name'] = "Hello2";
$now[1]['si'] = "BumBum2";
$now[2]['name'] = "Hello3";
$now[2]['si'] = "BumBum3";
$now[3]['name'] = "Hello4";
$now[3]['si'] = "BumBum4";
echo json_encode($now)."<br>";
unset($now[0]);
echo json_encode($now);
And the output:
[{"name":"Hello1","si":"BumBum1"},{"name":"Hello2","si":"BumBum2"},{"name":"Hello3","si":"BumBum3"},{"name":"Hello4","si":"BumBum4"}]
{"1":{"name":"Hello2","si":"BumBum2"},"2":{"name":"Hello3","si":"BumBum3"},"3":{"name":"Hello4","si":"BumBum4"}}
And my the JSON file turns to messy code. Appears numbers and etc.
Any ideas how to solve this.
You need to "reindex" the array (use array_values() function).
//unset..
unset($now[0]);
//reindex
$now = array_values($now);
//display as before
echo json_encode($now);

How To Pull JSON Data Using PHP

I'm trying to create a very simple php script that can pull data from a JSON file (array?) and echo it to the page. Sadly, I'm a complete newbie when it comes to PHP.
My goal is to dump all IP addresses and the corresponding client version into an output like this...
"127.0.0.1" "/Satoshi:0.9.1/"
"127.0.0.2" "/Satoshi:0.9.0/"
"127.0.0.3" "/Satoshi:0.9.0/"
"127.0.0.4" "/Satoshi:0.9.1/"
I can get the code to dump all data, but I'm not sure how to pull the ip and version without the ip and client version being named.. If that even makes sense?
Here is the code. What do I need to make it dump the correct data?
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$JSON = file_get_contents($url);
echo $JSON;
$data = json_decode($JSON);
var_dump($data);
?>
Thanks for your help!
Something like
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
$data = (array)$data; // cast from stdClass to array, as results have int keys
foreach($data['nodes'] as $results){
echo $results[0]. " ". $results[3]."<br />";
}
?>
It doesn't makes much sense to convert an array of hashes into the one of some non-named entities. I guess that all you need is to dump it in the following way:
foreach($data as $t)
{
echo("ip=".$t->ip." ua=".$t->ua);
}
I don't think there is any reason to cast to an array. Just process the returned object. This one has the double quotes as requested as well.
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$json = file_get_contents($url);
$data = json_decode($json);
foreach($data->nodes as $results){
echo "\"".$results[0]."\" \"".$results[3]."\"<br />";
}
?>

Categories