Supposing I have an Array with x elements. I would like to loop through this Array in "tens" and add a run-Time variable. This means that each set of 10 Arrays will have a unique run-time. I am using the code below:
$time = '00:00:00';
$k = 0;
for ($i = 0; $i < count($agnt_arr); $i+=10) {
$temp = strtotime("+$k minutes", strtotime($time));
$runTime = date('H:i', $temp);
array_push($agnt_arr[$i], $runTime);
$k+=4;
}
Where $agnt_arr is an Array with the following structure :
Array
(
[0] => Array
(
[name] => User.One
[email] => User.One#mail.com
)
[1] => Array
(
[name] => User.Two
[email] => User.Two#mail.com
)
[2] => Array
(
[name] => User.Three
[email] => User.Three#mail.com
)
)
The problem I'm having is the run times are only added to the 10th element which is expected But I would like elements 0-9 to have the same run time and 10-20 etc. How would I achieve something like this??
Probably easier like this always adding runtime but updating it for each 10:
$time = '00:00:00';
foreach($agent_arr as $key => $value) {
if($key % 10 === 0) {
$temp = strtotime("+$k minutes", strtotime($time));
$runTime = date('H:i', $temp);
}
$agent_arr[$key]['runtime'] = $runTime;
}
Here's my overcomplicated solution(and probably unnecessary):
$new_array = array();
foreach(array_chunk($array, 10 /* Your leap number would go here */) as $k => $v)
{
array_walk($v, function($value, $key) use (&$new_array){
$value['time'] = $time;
$new_array[] = $value;
});
}
Related
I have an array, which has only one month dates.
$dates =array (
'2018-10-15',
'2018-10-16',
'2018-10-17',
'2018-10-13',
'2018-10-19',
'2018-10-10',
'2018-10-11',
'2018-10-12',
'2018-10-22',
'2018-10-23',
'2018-10-29',
);
And script below
usort($dates,function($a,$b){
return strtotime($a) - strtotime($b);
});
$consecutive_added_set = [];
$consecutive_array = [];
$temp = [];
$temp[] = date('Y-m-d',strtotime($dates[0]));
for($i=1;$i<count($dates);++$i){
if(strtotime($dates[$i]) - strtotime($dates[$i - 1]) === 86400){ // 1 day gap(86400 seconds)
$temp[] = date('Y-m-d',strtotime($dates[$i]));
$consecutive_added_set[$dates[$i-1]] = true;
$consecutive_added_set[$dates[$i]] = true;
}else{
if(count($temp) > 1){
$consecutive_array[] = $temp;
}
$temp = [];
$temp[] = date('Y-m-d',strtotime($dates[$i]));
}
}
if(count($temp) > 1){ // the last consecutiveness match of dates as well(corner case)
$consecutive_array[] = $temp;
}
$conseq[] = []; // reset the array structure
$conseq['consecutive'] = $consecutive_array;
$conseq['consecutive_count'] = count($consecutive_array);
$conseq['non_consecutive'] = [];
foreach($dates as $current_date){
if(!isset($consecutive_added_set[$current_date])){ // skip all dates which were d for consecutiveness
$conseq['non_consecutive'][] = date('Y-m-d',strtotime($current_date));
}
}
Which is sorting and separating consecutive and non-consective dates. Currently in consecutive array it is showing all dates by group. But I just would like to show from and to dates. Here is output of above script
Array
(
[0] => Array
(
)
[consecutive] => Array
(
[0] => Array
(
[0] => 2018-10-10
[1] => 2018-10-11
[2] => 2018-10-12
[3] => 2018-10-13
)
[1] => Array
(
[0] => 2018-10-15
[1] => 2018-10-16
[2] => 2018-10-17
)
[2] => Array
(
[0] => 2018-10-22
[1] => 2018-10-23
)
)
[consecutive_count] => 3
[non_consecutive] => Array
(
[0] => 2018-10-19
[1] => 2018-10-29
)
)
My desired output
[consecutive] => Array
(
['dates1'] => Array
(
[0] => 2018-10-10
[3] => 2018-10-13
)
['dates2'] => Array
(
[0] => 2018-10-15
[2] => 2018-10-17
)
['dates3'] => Array
(
[0] => 2018-10-22
[1] => 2018-10-23
)
)
I have tried a lot to do it.
A quick workaround would be to get the first and last item in the array.
$consecutive_array = array_map(function($e){
if(!is_array($e)){
return $e;
}
$last = end($e);
return [reset($e), $last];
}, $consecutive_array);
Or as suggested in the comments use min() max() functions.
$consecutive_array = array_map(function($e){
if(!is_array($e)){
return $e;
}
return [min($e), max($e)];
}, $consecutive_array);
Important: don't rely on strtodate of a day being exactly 86400 seconds less than the strtodate of the next day - depending on the locale of the server, there will be daylight saving, which can mess this up!
In cases like this, I tend to compare strtodate('+1 day',$timestampOfDay1) to strtodate($timestampOfDay2) - this will include daylight saving.
Here's how I would do it:
//the dates
$dates =array (
'2018-10-15',
'2018-10-16',
'2018-10-17',
'2018-10-13',
'2018-10-19',
'2018-10-10',
'2018-10-11',
'2018-10-12',
'2018-10-22',
'2018-10-23',
'2018-10-29',
);
//call the function
$result = getConsecutive($dates);
//output
var_dump($result);
function getConsecutive($dates) {
sort($dates);
$result = [
'consecutive' => [],
'consecutive_count'=>0,
'non_consecutive' => []
];
$currentStart = null;
$currentTimestamp = null;
for ($i=0; $i<count($dates); $i++) {
$timestamp = strtotime($dates[$i]);
if ($currentStart == null) {
//first timestamp - set it as start & current
$currentStart = $timestamp;
$currentTimestamp = $timestamp;
} else if (strtotime('+1 day',$currentTimestamp) == $timestamp) {
//consecutive - keep waiting for a non-consecutive
$currentTimestamp = $timestamp;
} else {
if ($currentTimestamp == $currentStart) {
//we just got one
$result['non_consecutive'][] = date('Y-m-d',$currentTimestamp);
} else {
//we just got more then one, so they were consecutive
$result['consecutive']['dates'.(count($result['consecutive'])+1)] = [
date('Y-m-d',$currentStart),
date('Y-m-d',$currentTimestamp)
];
}
$currentStart = $timestamp;
$currentTimestamp = $timestamp;
}
}
//process the last timestamp
if ($currentTimestamp == $currentStart) {
//we just got one
$result['non_consecutive'][] = date('Y-m-d',$currentTimestamp);
} else {
//we just got more then one, so they were consecutive
$result['consecutive']['dates'.(count($result['consecutive'])+1)] = [
date('Y-m-d',$currentStart),
date('Y-m-d',$currentTimestamp)
];
}
$result['consecutive_count'] = count($result['consecutive']);
return $result;
}
I just tried to parse my array that contains numbers separated with comma into numbers without the comma, but still in array form. But it didn't work.
My code:
$total = $this->input->post('total');
$arrTot = array_filter(array_slice($total, 20));
print_r($arrTot);
Array result:
Array
(
[0] => 10,000
[1] => 100,000
[2] => 200,000
)
My desired output was to erase the comma in all number:
Array
(
[0] => 10000
[1] => 100000
[2] => 200000
)
I've tried with something just like this but it seems not even close with my desired output:
$total = $this->input->post('total');
$arrTot = array_filter(array_slice($total, 20));
for ($i=0; $i < count($arrTot); $i++) {
$valTot=str_replace( ',', '', $arrTot[$i]);
print_r($valTot);
}
Is there any way to solve this problem?
Thanks.
You can use array_walk to process each of the values in the array:
$arrTot = array('10,000', '100,000', '200,000');
array_walk($arrTot, function (&$v) {
$v = str_replace(',', '', $v);
});
print_r($arrTot);
Output:
Array
(
[0] => 10000
[1] => 100000
[2] => 200000
)
Demo on 3v4l.org
you might assign new value to current variable.
$arrTot = array_filter(array_slice($total, 20));
for ($i=0; $i < count($arrTot); $i++) {
$arrTot[$i]=str_replace( ',', '', $arrTot[$i]);
}
print_r($arrTot);
If you want the desired output, you need to replace the elements in the main array without comma.
$total = $this->input->post('total');
$arrTot = array_filter(array_slice($total, 20));
foreach ($arrTot as $key => $aTot) {
$arrTot[$key] = str_replace(',','',$arrTot[$i);
}
var_dump($arrTot);
Try this-
echo "<pre>";
$arr = array('10,000','100,000','200,000');
print_r($arr);
//result
Array
(
[0] => 10,000
[1] => 100,000
[2] => 200,000
)
foreach ($arr as $key => $value) {
$new[] = str_replace(',','',$value);
}
print_r($new);
Array
(
[0] => 10000
[1] => 100000
[2] => 200000
)
try this ,
$arr = ['10,000','100,000','200,000'];
foreach($arr as $key=>$val){
$arr[$key] = (int)str_replace(',','',$val);
}
var_dump($arr);
You could simply use str_replace to achieve desired result
$arrTot = array('10,000', '100,000', '200,000');
foreach($arrTot as $key => $value){
$arrTot[$key] = str_replace(",","",$value);
}
print_r($arrTot);
I have this sorting algorithm to sort the $days in reference to $daysOfWeek but I'm not sure why the last value is not being include to my array. Here is the code below
#default array
$daysOfWeek = array('M','T','W','TH','F');
#your array
$days = array('T','W','TH','','');
#create a new array with key association property
$daysAux = array();
foreach($days as $k=>$v) {
$key = array_search($v, $daysOfWeek);
if($key !== FALSE) {
$daysAux[$key] = $v;
}
else
$daysAux[$key] = '';
}
# array before sort
echo '<pre/>';print_r($daysAux);
ksort($daysAux);
$days = $daysAux;
#final result
echo '<pre/>';print_r($days);
#output
Array
(
[1] => T
[2] => W
[3] => TH
[0] =>
)
Array
(
[0] =>
[1] => T
[2] => W
[3] => TH
)
EXPECTED OUTPUT:
Array
(
[0] =>
[1] => T
[2] => W
[3] => TH
[4] =>
)
your code code should say $daysAux[$k] = instead of $daysAux[$key] =
beacuse otherwise when $key is false it will just overwrite the same entry instead of creating a new one
also need to swap days and daysOfWeek in the loop.
#default array
$daysOfWeek = array('M','T','W','TH','F');
#your array
$days = array('T','W','TH','','');
#create a new array with key association property
$daysAux = array();
foreach($days as $k=>$v) {
$key = array_search($v, $daysOfWeek);
if($key !== FALSE) {
$daysAux[$key] = $v;
}
else
$daysAux[$key] = '';
}
# array before sort
echo '<pre/>';print_r($daysAux);
ksort($daysAux);
$days = $daysAux;
#final result
echo '<pre/>';print_r($days);
I have looked and googled many times I found a few posts that are simular but I can not find the answer Im looking for so I hope you good people can help me.
I have a function that returns a simple number array. The array number values are dynamic and will change most frequently.
e.g.
array(12,19,23)
What I would like to do is take each number value in the array, compare it to a set range and return all the lower value numbers up to and including the value number in the array.
So if I do this:
$array = range(
(11,15),
(16,21),
(22,26)
);
The Desired output would be:
array(11,12,16,17,18,19,22,23)
But instead I get back all the numbers in all the ranges.
array(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26)
What would be a simple solution to resolve this?
Try this code
$range = array(
array(11,15),
array(16,21),
array(22,26),
);
$array = array(12,19,23);
$result = array();
foreach($range as $key=>$value)
{
//$range1 =$range[$key];
$min = $range[$key][0];
$max = $range[$key][1];
for($i = $min;$i<=$max;$i++)
{
if($i <= $array[$key])
{
array_push($result,$i);
}
}
}
echo "<pre>";print_r($result);
Iterate over each element, find the the start and end values you need to include, and append them to the output array:
$a = array(12,19,23);
$b = array(
range(11,15),
range(16,21),
range(22,26)
);
$c = array();
foreach ($a as $k => $cap) {
$start = $b[$k][0];
$finish = min($b[$k][count($b[$k])-1], $cap);
for ($i = $start; $i <= $finish; $i++) {
$c[] = $i;
}
}
print_r($c);
prints
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)
My solution is probably not the most efficient, but here goes:
$numbers = array(12,19,23);
$ranges = array(
array(11,15),
array(16,21),
array(22,26)
);
$output = array();
// Loop through each of the numbers and ranges:
foreach($numbers as $num) {
foreach($ranges as $r) {
if ($num >= $r[0] && $num <= $r[1]) {
// This is the correct range
// Array merge to append elements
$output = array_merge($output, range($r[0], $num));
break;
}
}
}
// Sort the numbers if you wish
sort($output, \SORT_NUMERIC);
print_r($output);
Produces:
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)
I have 2 arrays in PHP. One of them holds a list of dates, the other a list of numbers.
Array1
(
[0] => 2010-06-14
[1] => 2010-06-14
[2] => 2010-06-14
[3] => 2014-01-26
[4] => 2014-01-26
)
Array2
(
[0] => 120
[1] => 100
[2] => 60
[3] => 140
[4] => 30
)
The value [0] in Array2 belongs with the date [0] in Array1. What I am trying to do is add all of the values in Array2 together, based on the date. Any dates that match should have their values added together. So for example at the end I would like something like:
$date = 2010-06-14;
$value = 280;
$date = 2014-01-26;
$value = 170;
...and so on.
I've searched though the site but was unable to find exactly what I needed. Any help would be appreciated...
You can iterate $values, and get the corresponding date from $dates to use as the key in your result array.
foreach ($values as $key => $value) {
$result[$dates[$key]] = $value + ($result[$dates[$key]] ?? 0);
}
The output will be like this:
array (size=2)
'2010-06-14' => int 280
'2014-01-26' => int 170
$sum=0; // New Element
$Array3[][]=0; // New 2D array
$p=0; // Counter for 2D array
for($i=0;$i<5;$i++) // Single loop for traversing
{
$date=Array1[$i]; // Start for a date
while($date==Array1[$i]){ // For for Similar date
$sum=$sum+Array2[$i]; // Adding values of similar date
$i++; // Increment array
}
$Array3[$p]["date"]=$date; // Array3 date element
$Array3[$p]["sum"]=$sum; // Array4 date element
$i--; // Reducing a value which is incremented in while loop
}
Array3 is like
Array3
(
[0] => array( 'date' => " ",'sum' => " ")
[1] => array( 'date' => " ",'sum' => " ")
)
Are you trying to count all of the values in Array2 that have an entry in Array1 that matches some predefined target value?
If so, this for loop version should work:
private function forLoopVersion($array1, $array2, $target) {
$result = 0;
for ($i = 0; $i < count($array1); ++$i) {
if ($array1[$i] == $target) {
$result += $array2[$i];
}
}
return $result;
}
Also, this foreach loop version might work, but I do not know if the $key for $array1 can be used to index an element in $array2. You could try it:
private function foreachLoopVersion($array1, $array2, $target) {
$result = 0;
foreach ($array1 as $key => $value) {
if ($value == $target) {
$result += $array2[$key];
}
}
return $result;
}
$newArray = array();
for($i = 0; $i < count(Array1); $i++) {
$newArray[$Array1[$i]] = $Array2[$i];
}
echo $newArray[$date1] + $newArray[$date2];
Put the dates as keys to for easy math.