I need to get some data out of my influxdb database.
My current query is:
SELECT value FROM first,second,third WHERE location = 'somewhere' ORDER BY time DESC LIMIT 1
With this result:
array(3) {
[0]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:49.461551353Z"
["value"]=>
float(8.7572979625)
}
[1]=>
array(2) {
["time"]=>
string(29) "2020-02-06T12:44:48.70683539Z"
["value"]=>
float(22.5172978864)
}
[2]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:48.711272393Z"
["value"]=>
float(43.0572978868)
}
}
To process this information i have to use a while loop of some sort, i am unsure since i cannot find an
example of this online anywhere related to this type of data.
But to make the loop useful i need to know what the measurement name is, if i dont have that the result is quite unusable.
I would require this to be:
array(3) {
[0]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:49.461551353Z"
["measurement"]=>
string(5) "first"
["value"]=>
float(8.7572979625)
}
[1]=>
array(2) {
["time"]=>
string(29) "2020-02-06T12:44:48.70683539Z"
["measurement"]=>
string(6) "second"
["value"]=>
float(22.5172978864)
}
[2]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:48.711272393Z"
["measurement"]=>
string(5) "third"
["value"]=>
float(43.0572978868)
}
}
How can i achieve this and process the results correctly?
This solved my issue:
<?php
include "vendor/autoload.php";
$columns = array('first', 'second', 'third');
$cstrings = implode(",",$columns);
$database = InfluxDB\Client::fromDSN(sprintf('influxdb://xxx:xxx#%s:%s/%s', 'localhost', 8086, 'xxxx'));
$client = $database->getClient();
$result = $database->query("SELECT value FROM $cstrings WHERE location = 'xxx' ORDER BY time DESC LIMIT 1");
foreach ($columns as $column) {
$points = $result->getPoints($column);
print($column." ".$points[0]['value']."\n");
}
Related
I have this array :
array(8) {
[0]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(5) "82.00"
}
[1]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(5) "62.00"
}
[2]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(6) "182.00"
}
[3]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(6) "162.00"
}
[4]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(6) "103.00"
}
[5]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(5) "63.00"
}
[6]=>
array(2) {
["ticket_type"]=>
string(9) "normal"
["ticket_price"]=>
string(6) "203.00"
}
[7]=>
array(2) {
["ticket_type"]=>
string(9) "cheaper"
["ticket_price"]=>
string(6) "163.00"
}
}
I want to get the min and max price for "normal" and "cheap" ticket category, there will be more ticket categories, so can't hard code it, will get from DB, how can I do that? I'm using PHP 5.6 now, need export as array or json, Let me know if need more details.
Thank you
You can do it at the SQL level:
SELECT MIN(ticket_price) AS min_ticket_price, MAX(ticket_price) as max_ticket_price, ticket_type
FROM {your_table_name}
WHERE {your_conditions}
GROUP BY ticket_type
If you still want to do it in PHP you can just loop the array and to sort the ticket types then get the min and max values.
foreach($arr as $ticket){
$new[$ticket["ticket_type"]][] = $ticket["ticket_price"];
}
// loop the new array to only get the max and min values of each ticket type
foreach($new as $key => $n){
$res[$key]["max"] = max($n);
$res[$key]["min"] = min($n);
}
output:
array(2) {
["normal"]=>
array(2) {
["max"]=>
string(6) "203.00"
["min"]=>
string(5) "82.00"
}
["cheaper"]=>
array(2) {
["max"]=>
string(6) "163.00"
["min"]=>
string(5) "62.00"
}
}
https://3v4l.org/RcCHZ
Here is a solution to do on the php level:
$prices = [
["ticket_type"=>"normal", "ticket_price"=>"82.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"62.00"],
["ticket_type"=>"normal", "ticket_price"=>"182.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"162.00"],
["ticket_type"=>"normal", "ticket_price"=>"103.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"63.00"],
["ticket_type"=>"normal", "ticket_price"=>"203.00"],
["ticket_type"=>"cheaper", "ticket_price"=>"163.00"],
];
// STEP:1 group by ticket types
$grouped_array = [];
foreach($prices as $price) {
$ticket_type = $price["ticket_type"];
$grouped_array[$ticket_type][] = $price["ticket_price"];
}
/*
$grouped_array = [
"normal" => ["82.00", "182.00", "103.00", "203.00"],
"cheaper => ["62.00", "162.00", "63.00", "163.00"]
];
*/
function minMax($type_array) {
$min_max = ['min'=> min($type_array), 'max'=>max($type_array)];
return $min_max;
}
// STEP 2: find min and max in each category
$min_max = [];
foreach($grouped_array as $type => $prices) {
$min_max[$type] = minMax($prices);
}
/*
$min_max = [
"normal" => ["min"=>"82.00", "max"=>"203.00"],
"cheaper => ["min"=>"62.00", "max"=>"163.00"]
];
*/
However, doing it on the database layer is best in your case. this answers "How to get max and min value in a multidimensional array".
I am trying to parse a csv file in php to try and figure out how to get all data between some dates and the sort according to the date in ascending order but I am not able to sort the date column or I have no idea what format it is in please help
ID Reg Date FirstName LastName
1 1278336015 Sergio Roberto
2 1395656121 Ray Wilkins
3 1300276526 Trueman Ted
4 1374087492 Volt John
Please assist thanks
Okay, just for fun.
1. Parse the Data:
You could load the CSV data into an associative array like this (admittedly not very sophisticated, no error checking, validation etc):
$file = file_get_contents('CSV.csv'); // load csv into string
$rows = explode(PHP_EOL, $file); // break each line into array
$data = Array(); // where we'll store the data
foreach ($rows as $i => $row) {
$fields = explode(',', $row); // break each field into array
foreach ($fields as $col => $val) {
if ($i == 0) {
$names[] = $val; // headers in first row
} else {
$colName = $names[$col];
if ($colName == 'Reg Date') $val = date('Y-m-d H:i:s', $val);
$data[$i - 1][$colName] = $val;
}
}
}
So with your sample data above, you get:
array(4) {
[0]=>
array(4) {
["ID"]=>
string(1) "1"
["Reg Date"]=>
string(19) "2010-07-05 06:20:15"
["FirstName"]=>
string(6) "Sergio"
["LastName"]=>
string(7) "Roberto"
}
[1]=>
array(4) {
["ID"]=>
string(1) "2"
["Reg Date"]=>
string(19) "2014-03-24 03:15:21"
["FirstName"]=>
string(3) "Ray"
["LastName"]=>
string(7) "Wilkins"
}
[2]=>
array(4) {
["ID"]=>
string(1) "3"
["Reg Date"]=>
string(19) "2011-03-16 04:55:26"
["FirstName"]=>
string(7) "Trueman"
["LastName"]=>
string(3) "Ted"
}
[3]=>
array(4) {
["ID"]=>
string(1) "4"
["Reg Date"]=>
string(19) "2013-07-17 11:58:12"
["FirstName"]=>
string(4) "Volt"
["LastName"]=>
string(4) "John"
}
}
2. Sort the Data:
You can use usort() to sort by the timestamp column.
usort($data, function ($a, $b) {
if ($a['Reg Date'] == $b['Reg Date']) return 0;
return ($a['Reg Date'] < $b['Reg Date']) ? -1 : 1;
});
Results:
===After Sorting:===
array(4) {
[0]=>
array(4) {
["ID"]=>
string(1) "1"
["Reg Date"]=>
string(19) "2010-07-05 06:20:15"
["FirstName"]=>
string(6) "Sergio"
["LastName"]=>
string(7) "Roberto"
}
[1]=>
array(4) {
["ID"]=>
string(1) "3"
["Reg Date"]=>
string(19) "2011-03-16 04:55:26"
["FirstName"]=>
string(7) "Trueman"
["LastName"]=>
string(3) "Ted"
}
[2]=>
array(4) {
["ID"]=>
string(1) "4"
["Reg Date"]=>
string(19) "2013-07-17 11:58:12"
["FirstName"]=>
string(4) "Volt"
["LastName"]=>
string(4) "John"
}
[3]=>
array(4) {
["ID"]=>
string(1) "2"
["Reg Date"]=>
string(19) "2014-03-24 03:15:21"
["FirstName"]=>
string(3) "Ray"
["LastName"]=>
string(7) "Wilkins"
}
}
3. Filter the Data:
And you can use array_filter to filter the results down. I created a class to handle this (as described by #jensgram answer here)
$from = '2011-01-01';
$to = '2013-12-31';
$filtered = array_filter($data, Array(new compareDates($from, $to), 'isInRange'));
Results:
===After Filtering [2011-01-01 - 2013-12-31]:===
array(2) {
[1]=>
array(4) {
["ID"]=>
string(1) "3"
["Reg Date"]=>
string(19) "2011-03-16 04:55:26"
["FirstName"]=>
string(7) "Trueman"
["LastName"]=>
string(3) "Ted"
}
[2]=>
array(4) {
["ID"]=>
string(1) "4"
["Reg Date"]=>
string(19) "2013-07-17 11:58:12"
["FirstName"]=>
string(4) "Volt"
["LastName"]=>
string(4) "John"
}
}
Here's the simple class I used:
class compareDates
{
function __construct($from, $to)
{
$this->from = $from;
$this->to = $to;
}
function isInRange($ele)
{
if ($ele['Reg Date'] >= $this->from && $ele['Reg Date'] <= $this->to) {
return TRUE;
}
return FALSE;
}
}
NOTE: for the purpose of being able to see the date/time, I stored as
a string. But you would normally just use the numeric value to sort
by. I mention this to say that if you change the format of the date
string, it could break the sorting.
need some help with splitting mysql single column query array into different php variables here.
example:
here's the query, it's pretty simple to be honest.
but, i'm running out of ideas right now.
$string = "select Description from tblQuestion
where Employeeid = '$param'"
$query = $this->db->query($string);
$result = return $query->result_array();
btw, i am using Codeigniter and i tried to var_dump and the results are like this.
array(9) { [0]=> array(1) { ["Description"]=> string(5) "tidak" } [1]=> array(1) { ["Description"]=> string(5) "tidak" } [2]=> array(1) { ["Description"]=> string(5) "tidak" } [3]=> array(1) { ["Description"]=> string(5) "tidak" } [4]=> array(1) { ["Description"]=> string(5) "tidak" } [5]=> array(1) { ["Description"]=> string(5) "tidak" } [6]=> array(1) { ["Description"]=> string(5) "tidak" } [7]=> array(1) { ["Description"]=> string(5) "tidak" } [8]=> array(1) { ["Description"]=> string(5) "tidak" } }
i tried to use json_encode and the result is
[{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"}]
the question is.
how do i convert this stack of arrays into different variables like this?
$var0 = "tidak";
$var1 = "tidak";
$var2 = "tidak";
$var3 = "tidak";
and on and on....
thanks in advance.
cheers!
Put the results in a foreach loop and assign the values to a dynamic variable...
sample code like,
foreach($results as $key=>$val){
$str = 'var'.$key;
$$str = $val['Description'];
}
echo $var0;
Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>
I'm posting an array of ids and want to loop over those values. I'm trying the following to populate an array with key/value pairs but it looks like the array is coming out empty.
$arr = array();
foreach($_POST['ids'] as $id)
{
$arr[$id] = GetStuff($id);
}
UPDATE: Looks like array was populated fine. I'm trying to return contents of array by doing echo json_encode($arr) but response is blank.
Here is output of var_dump($_POST);
array(1) {
["ids"]=>
array(18) {
[0]=>
string(6) "156795"
[1]=>
string(6) "156800"
[2]=>
string(4) "4292"
[3]=>
string(6) "796053"
[4]=>
string(6) "660520"
[5]=>
string(4) "4293"
[6]=>
string(4) "4287"
[7]=>
string(6) "488339"
[8]=>
string(6) "837701"
[9]=>
string(7) "1152093"
[10]=>
string(7) "1186434"
[11]=>
string(7) "1324432"
[12]=>
string(6) "796051"
[13]=>
string(6) "144860"
[14]=>
string(5) "15065"
[15]=>
string(7) "1324434"
[16]=>
string(5) "13066"
[17]=>
string(4) "6969"
}
}
foreach($_POST['ids'] AS $i=>$id) {
//do stuff
}
Don't forget about quotes..
$arr = array();
foreach($_POST['ids'] as $id)
{
$arr[$id] = GetStuff($id);
}
Notice the tick marks around ids in $_POST.
This should work:
foreach($_POST['ids'] as $id)
{
$arr[$id] = $_POST['ids'][$id];
}
or even faster, if you are just wanting an array identical to the posted ids:
$arr = $_POST['ids'];
unless I am misunderstanding the question.