How to get max time form array elements - php

I want to get highest time from time_array.In below code I manually gave timings it is wrong.
time_array is like this,
Array (
[0] => Array (
[1] => Array ( [date] => 2016-04-09 [time] => 0900 [after_time] => 1100
[2] => Array ( [date] => 2016-04-10 [time] => 1100 [after_time] => 1300
[3] => Array ( [date] => 2016-04-11 [time] => 1300 [after_time] => 1500
)
)
my code,
<?php
foreach ($time_array AS $row) {
for($j=$start; $j < $limit ; $j++){
if($row[$start]['time']=='1300' && $row[$start]['after_time']=='1500' ){
//code
}else{
//code
}
}
}
?>

If you are using foreach
check this once
foreach ($time_array as $arr){
$ar = array_column($arr, 'time');
}
echo max($ar);

Related

PHP push missing weekdays with 0 total into array

I need to buildup a graph with total sales for each day for last week, I did not get all 7 days data, rather getting 3 days data from database, I need to push rest of the 4days with total sales 0,
I have the following array of 3 days
Array
(
[0] => Array
(
[SalesDate] => Jun09
[total] => 4
)
[1] => Array
(
[SalesDate] => Jun11
[total] => 2
)
[2] => Array
(
[SalesDate] => Jun14
[total] => 1
)
)
but I need all 7days data from Jun09 to Jun15 like this
Array
(
[0] => Array
(
[SalesDate] => Jun09
[total] => 4
)
[1] => Array
(
[SalesDate] => Jun10
[total] => 0
)
[2] => Array
(
[SalesDate] => Jun11
[total] => 2
)
[3] => Array
(
[SalesDate] => Jun12
[total] => 0
)
[4] => Array
(
[SalesDate] => Jun13
[total] => 0
)
[5] => Array
(
[SalesDate] => Jun14
[total] => 1
)
[6] => Array
(
[SalesDate] => Jun15
[total] => 0
)
)
I have tried with following code, but not getting the required data.
<?php
$sales =array(array('SalesDate' => 'Jun09',
'total' => 4
),
array
(
'SalesDate' => 'Jun11',
'total' => 2
),
array
(
'SalesDate' => 'Jun14',
'total' => 1
)
);
$final_array = array();
foreach($sales as $sale)
{
for($i=7;$i>0;$i--)
{
$current_weeks =[];
if($sale['SalesDate'] ==date('Md', strtotime("-".$i." days")))
{
$week_days['SalesDate'] = $sale['SalesDate'];
$week_days['total'] = $sale['total'];
}
else
{
$week_days['SalesDate'] = date('Md', strtotime("-".$i." days"));
$week_days['total'] = 0;
}
$final_array[] =$week_days;
}
}
You could first create a 'skeleton' array that matches your source array $arr, but with all the previous seven days $prevSevenDays. The date format matches the source array's, the totals are all set to 0.
// build empty skeleton
$prevSevenDays = [];
for ($i = 7; $i > 0; $i--) {
$prevSevenDays[$i]['SalesDate'] = date('Md', strtotime("-$i days"));
$prevSevenDays[$i]['Total'] = 0;
}
All you have to do is cycle through this array to replace the entries with available data in your source array $arr.
foreach ($arr as $sales) {
foreach ($prevSevenDays as $key => $day) {
if ($sales['SalesDate'] === $day['SalesDate']) $prevSevenDays[$key]['Total'] = $sales['Total'];
}
}
demo

Increment count for value in foreach loop PHP

I am trying to count the instances of rfid from one array to insert into another array so I can use this data to create charts.
At the moment my code is as follows
if ($message == "Broken" && $message_type == "BreakBeam" && $previous==$cat_id) {
$total = 1;
$splitTimeStamp = explode(" ",$eventtime);
$date = $splitTimeStamp[0];
$time = $splitTimeStamp[1];
$events[$c]['date'] = $date;
$events[$c]['device'] = $device;
$events[$c]['time']= $time;
$events[$c]['rfid']= $previous;
$events[$c]['count']=$total;
$c++;
}
}
$a = array();
$i=0;
foreach($events as $event){
if(isset($a[$event['rfid']])){
$a['rfid_id'][$event['rfid']]++;
}else{
$a['rfid_id'][$event['rfid']]=1;
}
if(isset($a[$event['date']])){
$a['dateinsert'][$event['date']]++;
}else{
$a['dateinsert'][$event['date']] =1;
}
}
$rfid = array();
// those are the ones we're going to put in!
foreach($a as $key => $count) {
foreach($count as $eventdetails['rfid_id'] => $event){
// so $key is the [rfid] key of the entry and $count is the count (all 1's?) in it
if (isset($rfid[$key])) {
$rfid[$key]+=$event;
}
else {
$rfid[$key]=$event;
}
}
}
Which outputs as follows :
Array
(
[0] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:51:37
[rfid] => 23641
[count] => 1
)
[1] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:52:20
[rfid] => 5609
[count] => 1
)
[2] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:53:23
[rfid] => 5609
[count] => 1
)
[3] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 16:02:44
[rfid] => 5609
[count] => 1
)
)
Array
(
[rfid_id] => Array
(
[23641] => 1
[5609] => 1
)
[dateinsert] => Array
(
[2020-09-17] => 1
)
)
Array
(
[rfid_id] => 2
[dateinsert] => 1
)
Ideally, I would like to achieve the following :
Array
(
[rfid_id] => Array
(
[23641] => 1
[5609] => 3
)
[dateinsert] => Array
(
[2020-09-17] => 1
)
)
or something to that effect, where I can look at the date, rfid codes and the time each was read on that date.
You simply missing the path in the assignment loop.
Same goes for rfid and date so I just explain on rfid.
You check the isset on one path but is not exist set on another - this cause the path to never exist - that why you keep getting 1's.
Look at:
foreach($events as $event){
if(isset($a[$event['rfid']])){
$a['rfid_id'][$event['rfid']]++;
} else {
$a['rfid_id'][$event['rfid']]=1;
}
}
Notice $a[$event['rfid']] is NOT the same as $a['rfid_id'][$event['rfid']].
You should change the if statement to be: if (isset($a['rfid_id'][$event['rfid']))

Select first working day from multidimensional array

I have a multidimensional array as shown below. in the day's array, it has various days which has both working and non-working days. now I want to consider first type=" working" as the start_date.
could you help me. thanks
Array
(
[error] => 0
[data] => Array
(
[start_date] => 2018-03-11
[end_date] => 2018-03-21
[days] => Array
(
[0] => Array
(
[type] => non_working
[sub_type] => weekend
[sub_sub_type] =>
[date] => 2018-03-11
)
[1] => Array
(
[type] => working
[sub_type] =>
[sub_sub_type] =>
[full_date] => 2018-03-12
)
[2] => Array
(
[type] => working
[sub_type] =>
[sub_sub_type] =>
[full_date] => 2018-03-13
)
)
)
)
I've tries this as of now:
$i=0;
$var = array();
foreach($arr['data']['days'][$i] as $var) {
if($var['type'] == 'working') {
break;
}
}
Rework $arr['data']['days'][$i] to $arr['data']['days']:
$start = null;
foreach($arr['data']['days'] as $var) {
if($var['type'] == 'working') {
$start = $var['date'];
break;
}
}

How to increment values which having same array element in php?

I am having following array and i want to increment quantity if date are same.
Array (
[0] => Array ( [date] => 2016-02-02 [quantity] => 2 )
[1] => Array ( [date] => 2016-02-04 [quantity] => 1 )
[2] => Array ( [date] => 2016-02-05 [quantity] => 1 )
[3] => Array ( [date] => 2016-02-02 [quantity] => 1 )
[4] => Array ( [date] => 2016-02-03 [quantity] => 1 )
[5] => Array ( [date] => 2016-02-02 [quantity] => 2 )
[6] => Array ( [date] => 2016-02-03 [quantity] => 2 )
[7] => Array ( [date] => 2016-02-04 [quantity] => 2 )
)
for example if 0 index having date 2016-02-02 and quantity 2,and same like 3rd index having same date but different quantity like wise 5th index. Now i want to add only the quantity if date are same and store into new array as
Array (
[0] => Array ( [date] => 2016-02-02 [quantity] => 5 )
[1] => Array ( [date] => 2016-02-04 [quantity] => 3 )
[2] => Array ( [date] => 2016-02-05 [quantity] => 1 )
[4] => Array ( [date] => 2016-02-03 [quantity] => 3 )
)
please explain me how to do such thing in php.
Try this way simply using foreach
$sum = array();
foreach ($array as $item) {
if (!isset($sum[$item['date']])) $sum[$item['date']] = 0;
$sum[$item['date']] += $item['quantity'];
}
print '<pre>';
print_r($sum);
OR
Using array_reduce()
$sum = array_reduce($array, function($result, $item) {
if (!isset($result[$item['date']])) $result[$item['date']] = 0;
$result[$item['date']] += $item['quantity'];
return $result;
}, array());
print '<pre>';
print_r($sum);
you can use array_search(); built in function in some way
a simple example:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
You can use this logic for above task.
foreach($arrayname as $key=>$value)
{
foreach($arrayname as $key1=>$value1)
{
if($value['date']==$value1['date']&&$key!=$key1)
{
$value['quantity']=$value['quantity']+$value1['quantity'];
unset($arrayname[$key]);
}
}
}

Combining arrays with common values

I have an array of dates that looks like this:
Array
(
[0] => '2014-01-01'
[1] => '2014-01-02'
[2] => '2014-01-03'
[3] => '2014-01-04'
[4] => '2014-01-05'
[5] => '2014-01-06'
[6] => '2014-01-07'
)
and I have another array of dates and counts that looks like this:
[All] => Array
(
[0] => Array
(
[count] => 2
[date] => 2014-01-06
)
[1] => Array
(
[count] => 1
[date] => 2014-01-03
)
[2] => Array
(
[count] => 43
[date] => 2013-12-11
)
[3] => Array
(
[count] => 103
[date] => 2013-12-10
)
[4] => Array
(
[count] => 128
[date] => 2013-12-09
)
[5] => Array
(
[count] => 75
[date] => 2013-12-08
)
[6] => Array
(
[count] => 107
[date] => 2013-12-07
)
I want to make a new associative array where all the keys are the dates from the first array above and all of the values are either the count matched up with the corresponding date or "0".
So for instance, the new array would look like this:
Array
(
[2014-01-01] => 0
[2014-01-02] => 0
[2014-01-03] => 1
[2014-01-04] => 0
[2014-01-05] => 0
[2014-01-06] => 2
[2014-01-07] => 0
)
Does that make sense? Please feel free to ask any questions you may have. Thank you!
Try this code:
$result = array();
foreach($firstArray as $f){
foreach($secondArray as $s){
if($s['date'] == $f) $result[$f] = $s['count'];
}
if(!array_key_exists($f, $result)) $result[$f] = 0;
}
$result = array();
foreach($secondArray as $s){
if(in_array($s['date'], $firstArray) {
unset($firstArray[$s['date']]);
$result[$s['date']] = $s['count'];
}
}
// if items left in first array that are not found within foreach:
if (!empty($firstArray))
$result = array_merge($result, array_fill_keys($firstArray, 0));
// sort by key so dates go ascending
ksort($result);
$new = array();
foreach($all as $row)
{
$new[$row['date']] = $row['count'];
}
array_merge ($new, $old);
Here $all is the array with the date and count indices.
$old is the exisisting array.
That's a 2-liner:
// use dates as index and set everything to 0
$result = array_fill_keys($x, 0));
// copy over existing counts
array_walk($all, function($v) use (&$result) { if (array_key_exists($v['date'], $result)) { $result[$v['date']] = $v['count'];}});

Categories