I have this set of unix timestamps:
1473804000 1471831200
I'm not able to assign a date in this format ('Y-m-d H:i:s') to each unix timestamp.
My code:
$eventhour = '1473804000';
$client = array(
'pt' => $pt,
'eventhour' => date('Y-m-d H:i:s', $eventhour),
'activepower' => $avg['system.avg(activepower)']
);
The eventhour key is empty, I guess the function date returned false. The unix timestamp is valid, why is it returning false?
My Loop code
foreach ($result as $row):
$pt = $row['pt'];
$statement = new \Cassandra\SimpleStatement("select avg(activepower) FROM datavalue_test_by_hour WHERE pt= :pt AND eventhour = :eventhour;");
$options = new \Cassandra\ExecutionOptions(
array('arguments' => array('pt' => $pt, 'eventhour' => $row['eventhour'])))
;
$data = $this->cassandra->executeWithOptions($statement, $options);
$eventhour = $row['eventhour'];
echo $eventhour . PHP_EOL;
$avg = $data->first();
$client = array(
'pt' => $pt,
'eventhour' => date('Y-m-d H:i:s', $eventhour),
'activepower' => $avg['system.avg(activepower)']
);
print_r($client);
//fputcsv($fp, $client);
endforeach;
My print_r():
1462554000
Array
(
[pt] => AFH0AEFB0BFDEI
[eventhour] =>
[activepower] => 0.02475
)
1474887600
Array
(
[pt] => A0A0AEAF0DI0H0
[eventhour] =>
[activepower] => 0.1115
)
1475244000
Array
(
[pt] => AFH0ADE0AB0GDI
[eventhour] =>
[activepower] => 0.0905
)
1473008400
Array
(
[pt] => A0A0AEAF0GDFHB
[eventhour] =>
[activepower] => 0.014
)
1470693600
Array
(
[pt] => AFH0AEE00AF0FG
[eventhour] =>
[activepower] => 0.051
)
1452200400
Array
(
[pt] => AFH0AEE00BACIC
[eventhour] =>
[activepower] => 0.152
)
1463637600
Array
(
[pt] => AFH0ACFB00CD0F
[eventhour] =>
[activepower] => 0.041
)
I resolved this issue by casting the returned $row['eventhour'] from the database to a INT.
$eventhour = (int) $row['eventhour'];
Is working now.
Related
It is correctly insert the value of $name[$init] into the key dsf under the $val['dsf'] but it is inerting only the last value in dsf under the $val['customProduct']['dsf']
$name = array(0=>'Amit',1=>'Amit1',2=>'Amit2');
foreach($order->orderLines as $init =>$val){
$val['dsf'] = $name[$init];
$val['customProduct']['dsf'] = $name[$init];
}
You need the $val pass by reference:
$name = array(0=>'Amit',1=>'Amit1',2=>'Amit2');
$orderLines = array(
array(
'dsf' => array(),
'customProduct' => array()
),
array(
'dsf' => array(),
'customProduct' => array()
),
array(
'dsf' => array(),
'customProduct' => array()
),
);
foreach($orderLines as $init => &$val){ //edit here
$val['dsf']= $name[$init];
$val['customProduct']['dsf'] = $name[$init];
}
print_r($orderLines);
Output:
Array
(
[0] => Array
(
[dsf] => Amit
[customProduct] => Array
(
[dsf] => Amit
)
)
[1] => Array
(
[dsf] => Amit1
[customProduct] => Array
(
[dsf] => Amit1
)
)
[2] => Array
(
[dsf] => Amit2
[customProduct] => Array
(
[dsf] => Amit2
)
)
)
See here
Please Try This and In Case If You Have Any Query Just Comment I Am Happy To Answer
<?php
$name = array(0=>'Amit',1=>'Amit1',2=>'Amit2');
$orderLines = array(
array(
"id"=>1,
"order_id"=>10,
"dfs"=>0,
"customProduct"=>array(
"id"=>102,
"order_id"=>10,
"dfs"=>0,
"name"=>""
)
),
array(
"id"=>2,
"order_id"=>20,
"dfs"=>1,
"customProduct"=>array(
"id"=>105,
"order_id"=>20,
"dfs"=>1,
"name"=>""
)
),
array(
"id"=>3,
"order_id"=>50,
"dfs"=>2,
"customProduct"=>array(
"id"=>107,
"order_id"=>50,
"dfs"=>2,
"name"=>""
)
)
);
$orderLinestemp = array();
foreach($name as $value){
$temp_array = array("dfs"=>$value,"customProduct"=>array("name"=>$value));
array_push($orderLinestemp, $temp_array);
}
$orderLines=array_replace_recursive($orderLines,$orderLinestemp);
echo "<pre/>";
print_r($orderLines);
?>
output
I'm trying to merge two arrays which have a custom key using array_push, but when I use array_push it removes the custom key.
For example if I just create a normal array with a custom key it works fine:
$price_arr = array();
$date = '2017-08-01';
$insert_data = array(
$date => array(
'adult_1' => '10'
)
);
print_r($insert_data);
The result is:
Array ( [2017-08-01] => Array ( [adult_1] => 10 ) )
However if I use array push it removes the custom key, for example:
$price_arr = array();
$date = '2017-08-01';
$insert_data = array(
$date => array(
'adult_1' => '10'
)
);
array_push($price_arr, $insert_data);
$insert_data = array(
$date => array(
'child_1' => '2'
)
);
array_push($price_arr, $insert_data);
print_r($price_arr);
The result is:
Array ( [0] => Array ( [2017-08-01] => Array ( [adult_1] => 10 ) ) [1] => Array ( [2017-08-01] => Array ( [child_1] => 2 ) ) )
The result I'm trying to produce is:
Array ( [2017-08-01] => Array ( [adult_1] => 1 [child_1] => 2 ) )
Any help appreciated!
why not just do
$arr['custom_key'] = 'your value';
you do are not bound to use array_push , just assign it and it is done.
$price_arr = array();
$date = '2017-08-01';
$price_arr[$date]['adult_1'] = 10;
$price_arr[$date]['child_1'] = 2;
print_r($price_arr);
You have to use array_merge instead of array_push
$price_arr = array();
$date = '2017-08-01';
$insert_data = array(
$date => array(
'adult_1' => '10'
)
);
$price_arr = array_merge($insert_data);
$insert_data = array(
$date => array(
'child_1' => '2'
)
);
$price_arr[$date] = array_merge($price_arr[$date],$insert_data[$date]);
echo "<pre>";
print_r($price_arr);
I am getting nested array reply. It is contain date, time and day I want to break that. How can i do that.
This is my reply
Array
(
[code] => 202
[message] => Accepted
[data] => Array
(
[result] => Array
(
[15:45~31-10-2016 Mon] => Array
(
[Sday] =>
[Ttime] => 15:45
[Smonth] =>
"[15:45~31-10-2016 Mon] => Array" how to assign variable and how can i break this into day ,date and time variable
As mentioned in Mohammad's comment on your question,
You should use preg_split function.
$str = key($array['data']['result']); // 15:45~31-10-2016 Mon
$res = preg_split("/[~\s]/", $str);
echo '<pre>'; print_r($res);
output:-
Array
(
[0] => 15:45
[1] => 31-10-2016
[2] => Mon
)
If you have only single element in result array, use extract and explode to retrieve the values from man array: Something like -
$result = $array['data']['result'];
$date = key($result);
$day = extract($result[$date]);
var_dump($date); // 15:45~31-10-2016 Mon
var_dump($Ttime); // will output 15:45
$date = explode(' ', $date);
$dateString = substr($date[0], strpos($date[0], "{$Ttime}~") + 1); //31-10-2016
$week = $date[1]; // var_dump($week); will give `Mon`
Given:
$result = array(
'code' => '202',
'message' => 'Accepted',
'data' => array(
'result' => array(
'15:45~31-10-2016 Mon' => array(
'Sday' => '',
'Ttime' => '15:45',
'Smonth' => ''
)
)
)
);
you can do this:
$data = $result['data']['result'];
$dateKey = key($data);
$dateString = preg_split("/[~\s]/", $dateKey);
$date = array(
'day' => $dateString[2],
'date' => $dateString[1],
'time' => $dateString[0]
);
var_dump($date);
or this:
$data = $result['data']['result'];
$dateKey = key($data);
$dateString = preg_replace("/[~\s]/", ' ', $dateKey);
$dateObj = DateTime::createFromFormat('H:i d-m-Y D', $dateString);
$date = array(
'day' => $dateObj->format('D'),
'date' => $dateObj->format('m-d-Y'),
'time' => $dateObj->format('H:i')
);
var_dump($date);
I have an array in php like this
Array (
[0] => Array (
[date] => 21/07/2014
[total_booking] => 1
)
[1] => Array (
[date] => 1/08/2014
[total_booking] => 1
)
[2] => Array (
[date] => 2/09/2014
[total_booking] => 2
)
)
if i get value $_POST['from_date'] and $_POST['to_date'] all array other than between this from_date,to_date should be removed.
Edit my code
foreach ($newarray as $newarray){
if ($newarray[Date] < $to_date && $from_date > $newarray[Date])
{
// i want to remove this row from array
}
}
Here's a solution using array_filter:
<?php
$data = array(
array(
'date' => '21/07/2014',
'total_booking' => '1'
),
array(
'date' => '1/08/2014',
'total_booking' => '1'
),
array(
'date' => '2/09/2014',
'total_booking' => '2'
)
);
$from_date = strtotime('01-08-2014'); //$_POST['from_date']
$to_date = strtotime('21-08-2014'); //$_POST['to_date']
$data = array_filter($data, function($array) use ($from_date, $to_date) {
$epoch = strtotime(str_replace('/', '-', $array['date']));
return $epoch >= $from_date && $epoch <= $to_date;
});
$data = array_values($data);
print_r($data);
Output:
Array
(
[0] => Array
(
[date] => 1/08/2014
[total_booking] => 1
)
)
Only one element is outputted because only 1/08/2014 is between 1/08/2014 and 21/08/2014.
For earlier PHP versions:
function filter($array) {
$from_date = strtotime('01-08-2014'); //$_POST['from_date']
$to_date = strtotime('21-08-2014'); //$_POST['to_date']
$epoch = strtotime(str_replace('/', '-', $array['date']));
return $epoch >= $from_date && $epoch <= $to_date;
}
$data = array_filter($data, 'filter');
I have this array in php returned from db
Array
(
[inv_templates] => Array
(
[0] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 1
[inven_template_name] => CopperWires6G
[constrained] => 0
[value_constraints] =>
[accept_range] => 2 - 16
[information] => Measured Manual
)
[1] => Array
(
[inven_subgroup_template_id] => 1
[inven_group] => Wires
[inven_subgroup] => CopperWires
[inven_template_id] => 2
[inven_template_name] => CopperWires2G
[constrained] => 0
[value_constraints] =>
[accept_range] => 1 - 7
[information] => Measured by Automated Calipers
)
)
)
I need to output this kind of multidimensional stuff
Array
(
[Wires] => Array
(
[inv_group_name] => Wires
[inv_subgroups] => Array
(
[CopperWires] => Array
(
[inv_subgroup_id] => 1
[inv_subgroup_name] => CopperWires
[inv_templates] => Array
(
[CopperWires6G] => Array
(
[inv_name] => CopperWires6G
[inv_id] => 1
)
[CopperWires2G] => Array
(
[inv_name] => CopperWires2G
[inv_id] => 2
)
)
)
)
)
)
I currently do this stuff
foreach ($data['inv_templates'] as $key => $value) {
$processeddata[$value['inven_group']]['inv_group_name'] = $value['inven_group'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_name'] = $value['inven_subgroup'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_name'] = $value['inven_template_name'];
$processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
EDIT : A var_export
array (
'inv_templates' =>
array (
0 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '1',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '2 - 16',
'information' => 'Measured Manual',
),
1 =>
array (
'inven_subgroup_template_id' => '1',
'inven_group' => 'Wires',
'inven_subgroup' => 'CopperWires',
'inven_template_id' => '2',
'inven_template_name' => 'CopperWires6G',
'constrained' => '0',
'value_constraints' => '',
'accept_range' => '1 - 7',
'information' => 'Measured by Automated Calipers',
),
),
)
The foreach is almost unreadable. There must be a simpler way
$processeddata = array();
foreach($data['inv_templates'] as $key => $value) {
$group = $value['inven_group'];
$processeddata[$group]['inv_group_name'] = $group;
$subgroup = &$processeddata[$group]['inv_subgroups'][$value['inven_subgroup']];
$subgroup['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
$subgroup['inv_subgroup_name'] = $value['inven_subgroup'];
$template = $value['inven_template_name'];
$subgroup['inv_templates'][$template]['inv_name'] = $template;
$subgroup['inv_templates'][$template]['inv_id'] = $value['inven_template_id'];
}
return $processeddata;
Untested code. This structures the array in a multidimensional way, and then uses array_merge_recursive to merge them with the already processed data.
if (!isset($processeddata[$value['inven_group']]))
$processeddata[$value['inven_group']] = array();
$processeddata[$value['inven_group']] = array_merge_recursive(
$processeddata[$value['inven_group']],
array(
'inv_group_name' => $value['inven_group'],
'inv_subgroups' => array(
$value['inven_subgroup'] => array(
'inv_subgroup_id' => $value['inven_subgroup_template_id'],
'inv_subgroup_name' => $value['inven_subgroup'],
'inv_templates' => array(
$value['inven_template_name'] => array(
'inv_name' => $value['inven_template_name'],
'inv_id' => $value['inven_template_id'],
),
),
),
),
)
);
I find this format usually works for me. You could do it more efficient, I've just never cared :D
I started traversing at $yourArray['inv_templates'] though.
function groupToStructure(array $rows, array $keys) {
$tree = array();
$keys = array_reverse($keys);
foreach ($rows as $row) {
$subTree = array($row);
foreach ($keys as $key) {
$subTree = array($row[$key] => $subTree);
}
$tree = array_merge_recursive($tree, $subTree);
}
return $tree;
}
print_r(groupToStructure($rows, array('inven_group', 'inven_subgroup', 'inven_template_name')));