I have an array with the hours of production like this (the number of array is not fixed, somethimes we can have production in January, not in february, and restart in March etc...) :
Array
(
[0] => Array
(
[Month] => 8
[HoursProd] => 181.37
)
[1] => Array
(
[Month] => 9
[HoursProd] => 699.35
)
[2] => Array
(
[Month] => 10
[HoursProd] => 500.25
)
[3] => Array
(
[Month] => 11
[HoursProd] => 350.61
)
)
I want to divide the hours in my array by the number of hours in the month. Get the month key (number of th month), calculate the number of hours in this month, and divide my Hours value by this number.
For the number of hours, i write this code for try to have the total of hours in a month.:
$m = 1;
$a = date('m');
$y = date("y");
$result2 = array();
for($m; $m <= 12; $m++){
$d=cal_days_in_month(CAL_GREGORIAN,$m,$y);
$result2[]["HoursTotalMonth"] = $d * 24;
$result2[]["Month"] = $m;
$t = $d * 24;
//To simplify the problem, i try to convert Hours of the month in the same array of production, and wheen the value of month is the same, divise value hours prod by value of hours total month
$test[] = array("Month" => $m, "HoursTotalMonth" => $t);
};
I try lot of things whithout success. The goal being at the end to be able to create a Json file to create a chart with ChartJS, The goal being at the end to be able to create a Json file to create a graph with ChartJS, with the production hour / hour ratio of the month in percentage. I succeed with a simple array but not with an associative multidimentional array.
Ex : [Month] => 8
[RatioHoursProdOnHoursTotalMonth] => 24.37 // (181.37 / (31j*24h))
I want this array :
Array
(
[0] => Array
(
[Month] => 8
[RatioHoursProdOnHoursTotalMonth] => 24.37
)
[1] => Array
(
[Month] => 9
[RatioHoursProdOnHoursTotalMonth] => 97.13
)
[2] => Array
(
[Month] => 10
[RatioHoursProdOnHoursTotalMonth] => 67.23
)
[3] => Array
(
[Month] => 11
[RatioHoursProdOnHoursTotalMonth] => 48.69
)
)
To be able to use the Json encoder, like this one:
[{"Month":8,"RatioHoursProdOnHoursTotalMonth":24.37},{"Month":9,"RatioHoursProdOnHoursTotalMonth":97.13},{"Month":10,"RatioHoursProdOnHoursTotalMonth":67.23},{"Month":11,"RatioHoursProdOnHoursTotalMonth":48.69}]
Is it what you expect as output ?
$a = date('m');
$y = date("y");
$result = array();
$data = array(100,60,75,90,58,98,105,85,74,685,700,550);
for ($m = 1; $m <= 12; $m++){
$d = cal_days_in_month(CAL_GREGORIAN, $m, $y);
$result[] = [
"Heure" => $d * 24,
"Mois" => $m,
"ratio" => $data[$m - 1] * 100 / ($d * 24),
];
};
print_r($result);
With the help of #Melvyn Marigny, the code below make the output i want.
$m = 1;
$a = date('m');
$y = date("y");
$nb = count($result);
for ($m = 1; $m <= $nb; $m++){
$d = cal_days_in_month(CAL_GREGORIAN, $m, $y);
$result3[] = [
"Mois" => $result[$m - 1]["Mois"],
"HeureProd" => $result[$m - 1]["Heure"],
"HeureMois" => $d * 24,
"ratio" => $result[$m - 1]["Heure"] / ($d * 24),
];
}
print_r($result3);
Array
(
[0] => Array
(
[Mois] => 8
[HeureProd] => 181.37
[HeureMois] => 744
[ratio] => 0.24377688172043
)
[1] => Array
(
[Mois] => 9
[HeureProd] => 753.39
[HeureMois] => 696
[ratio] => 1.0824568965517
)
[2] => Array
(
[Mois] => 10
[HeureProd] => 1292.25
[HeureMois] => 744
[ratio] => 1.7368951612903
)
[3] => Array
(
[Mois] => 11
[HeureProd] => 376.11
[HeureMois] => 720
[ratio] => 0.522375
)
Related
I have an array of time ranges like this:-
$events = array(
array("Monday", '19:00:00', '19:30:00', 0),
array("Monday", '19:10:00', '19:40:00', 0),
array("Tuesday", '19:10:00', '19:40:00', 0),
array("Wednesday", '19:10:00', '19:40:00', 0),
array("Monday", '19:30:00', '19:50:00', 0),
);
I am using bubble sort on the array:-
for($i = 0; $i < (count($events) - 1); $i++)
{
for($j = 1; $j < (count($events) - i - 1); $j++)
{
if($events[$i][0] < $events[$j][0])
{
if ($events[$j] > $events[($j + 1)])
{
$swap = $events[$j];
$events[$j] = $events[($j + 1)];
$events[($j + 1)] = $swap;
}
}
}
}
The result comes like this:-
Array (
[0] => Array (
[0] => Monday
[1] => 19:00:00
[2] => 19:30:00
[3] => 0)
[1] => Array (
[0] => Monday
[1] => 19:10:00
[2] => 19:40:00
[3] => 0)
[2] => Array (
[0] => Monday
[1] => 19:30:00
[2] => 19:50:00
[3] => 0)
[3] => Array (
[0] => Tuesday
[1] => 19:10:00
[2] => 19:40:00
[3] => 0)
[4] => Array (
[0] => Wednesday
[1] => 19:10:00
[2] => 19:40:00
[3] => 0)
)
Now I need to strike out those time ranges which overlap on a specific day.
Like this:
Array (
[0] => Array (
[0] => Monday
[1] => 19:00:00
[2] => 19:30:00
[3] => 1)
[1] => Array (
[0] => Monday
[1] => 19:10:00
[2] => 19:40:00
[3] => 1)
[2] => Array (
[0] => Monday
[1] => 19:30:00
[2] => 19:50:00
[3] => 1)
[3] => Array (
[0] => Tuesday
[1] => 19:10:00
[2] => 19:40:00
[3] => 0)
[4] => Array (
[0] => Wednesday
[1] => 19:10:00
[2] => 19:40:00
[3] => 0)
)
The ones which are [3] => 1, denote there is a time overlap conflict.
How can I proceed?
I tried to use this solution, but got no luck. Besides, mine has a day of week.
I had a little play and this is what I came up with. It preserves the original order of the $events array as textual days don't sort well (Friday would come before Wednesday etc). A bit brute force, not optimised or properly tested, but hope it's useful for ideas.
foreach ($events as $event_id=>$event) {
$days[$event[0]][]=[$event_id,$event[1],$event[2]];
}
foreach ($days as $dayevents) {
if (count($dayevents)>1) {
foreach ($dayevents as $dayevent1) {
foreach ($dayevents as $dayevent2) {
if ((($dayevent1[1]>$dayevent2[1]) and ($dayevent1[1]<$dayevent2[2])) or
(($dayevent1[2]>$dayevent2[1]) and ($dayevent1[2]<$dayevent2[2]))) {
$events[$dayevent1[0]][3]=1;
$events[$dayevent2[0]][3]=1;
}
}
}
}
}
$events = array(
array("Monday", '19:00:00', '19:30:00', 0),
array("Monday", '19:10:00', '19:40:00', 0),
array("Tuesday", '19:10:00', '19:40:00', 0),
array("Wednesday", '19:10:00', '19:40:00', 0),
array("Monday", '19:30:00', '19:50:00', 0),
);
$combined = array();
// first we collect all intervals for the given day
foreach($events as $record)
{
$combined[$record[0]][] = array(
$record[1], $record[2], 0
);
}
// then for each day we look if there are overlaps
foreach($combined as $day => &$intervals)
{
$len = count($intervals);
// we compare each interval with each of the rest intervals for the same day
foreach($intervals as $i => &$interval_1)
{
// we convert the start/end times of the interval A to an integer
$begin_1 = str_replace(':','',$interval_1[0]);
$end_1 = str_replace(':','',$interval_1[1]);
for($k = $i + 1; $k < $len; $k++)
{
// we convert the start/end times of the interval B to an integer
$begin_2 = str_replace(':','',$intervals[$k][0]);
$end_2 = str_replace(':','',$intervals[$k][1]);
// we compute the overlap of the 2 intervals
$overlap = max(0,$end_1 - $begin_1 - max(0,$end_1 - $end_2) - max(0,$begin_2 - $begin_1));
if($overlap)
{
$interval_1[2]++; // we increase the counter of interval A
$intervals[$k][2]++; // we increase the counter of interval B
}
}
}
}
<?php
$events = array(
array("Monday", '19:00:00', '19:10:00'),
array("Monday", '19:05:00', '19:20:00'),
array("Tuesday", '19:10:00', '19:40:00'),
array("Wednesday", '19:10:00', '19:40:00'),
array("Monday", '19:15:00', '19:30:00'),
);
function convertToSeconds($time){
$time = explode(":",$time);
return intval($time[0]) * 3600 + intval($time[1]) * 60 + intval($time[2]);
}
$time_ranges = [];
$hold_dates = [];
for($i=0;$i<=86401;++$i){
$time_ranges[] = 0;
$hold_dates[$i] = [];
}
$time_range_for_week = [];
$week_days = ['monday' ,'tuesday','wednesday','thursday','friday','saturday','sunday'];
foreach($week_days as $day){
$time_range_for_week[$day] = [
'hold_dates' => $hold_dates,
'time_range' => $time_ranges
];
}
foreach($events as &$data){
$start_time = convertToSeconds($data[1]);
$end_time = convertToSeconds($data[2]);
$time_range_for_week[strtolower($data[0])]['hold_dates'][$start_time][] = &$data;
$time_range_for_week[strtolower($data[0])]['hold_dates'][$end_time][] = &$data;
$time_range_for_week[strtolower($data[0])]['time_range'][$start_time] += 1;
$time_range_for_week[strtolower($data[0])]['time_range'][$end_time + 1] -= 1;
}
foreach($time_range_for_week as $day_name => &$day_data){
$sum = 0;
foreach($day_data['time_range'] as $time => $value){
$sum += $value;
if($sum > 1 && count($day_data['hold_dates'][$time]) > 0){
foreach($day_data['hold_dates'][$time] as &$each_event){
$each_event[3] = 1;
}
}
}
}
print_r($events);
Demo: https://3v4l.org/JeGdR
Algorithm:
This is rather simple and efficient than basic brute force.
To find out collisions between times, we first make an array of size 86400 for each day. 86400 because it is no. of seconds(24*60*60) for a day(like Monday or Tuesday etc).
The array looks like this for a particular day(like Monday or Tuesday etc):
Structure:
Array
(
[monday] => Array
(
[hold_dates] => Array
(
[],[],[],... till 86400
),
[time_range] => Array
(
[],[],[],... till 86400
),
),
[tuesday] => Array(
...
),
...
)
In the hold_dates key for a day, we are just going to have our array values at the start time and end times positions. Here start time is the 1st position(0-based indexing) in array("Monday", '19:10:00', '19:40:00', 0) and end time is 2nd position in array("Monday", '19:10:00', '19:40:00', 0). In the code, the lines look like:
Code:
$time_range_for_week[strtolower($data[0])]['hold_dates'][$start_time][] = &$data;
$time_range_for_week[strtolower($data[0])]['hold_dates'][$end_time][] = &$data;
The & is used for syncing updates with actual $events array, so it's a pass by reference.
The time_range will just hold integer values and those integer values as explained below.
Let's consider intervals like below:
[2,7],
[4,9],
[6,10]
We have a timeline like below:
1 2 3 4 5 6 7 8 9 10 11 // these are seconds from 1 to 11
+1 -1
+1 -1
+1 -1
1 1 2 2 3 3 2 2 1 0
In the above diagram, for each date's start time, we add +1, and -1 to it's end time + 1. This means that, when we iterate from 1 to 11 and keep summing values, if we find any date's start time or end time having a sum > 1, we found there is a collision and we need to set it's value to 1.
We do the same in the above code. Here, the space used for this code is 86400 * 7 = 604800, which is constant space O(1) since this does not depend upon size of $events.
Time complexity is again the constant iterations 86400 * 7 = 604800 + looping twice through each element in $events, which makes it O(n) where n is size of $events.
Given an array of arrays like this:
$array = array(
0 => array (
0 => 35,
1 => 30,
2 => 39
),
1 => array (
0 => 20,
1 => 12,
2 => 5
),
...
n => array (
0 => 10,
1 => 15,
2 => 7
),
);
I have the need to find the entry in the array which is closer to given parameters
find($a, $b, $c) {
//return the closer entry to the input
}
For closer entry I mean the entry which has closer values to the ones gave in input, e.g. passing (19, 13, 3) it should return $array[1]
The way in which I do the calculation at the moment is looping through the whole array, keeping a variable $distance which starts from -1, and a temporary $result variable. For each element I calculate the distance
$dist = abs( subarray[0] - $a ) + abs ( subarray[1] - $b ) + abs( subarray[2] - $c )
and if the calculated distance is equal to -1 or lower than the variable $distance which is out of the loop, I assign the new distance to the varaible and I save the corresponding array in the $result variable. At the end of the loop I end up having the value I need.
Also, one of the values can be empty: e.g. (19, 13, false) should still return $array[1] and the calculation should then ignore the missing parameter - in this case the distance is calculated as
$dist = abs( subarray[0] - $a ) + abs ( subarray[1] - $b );
ignoring the values of subarray[2] and $c.
The problem is, even if my code is working, it took too much time to execute as the size of the array can easily go up to many hundred thousands elements. We are still talking about milliseconds, but for various reasons this is still unacceptable.
Is there a more effective way to do this search in order to save some time?
A custom function - maybe there is a better way but check it out :
In a few words :
Search all the items and find in percentage the difference between the number it checks($mArray[0...3]) and the number you gave($mNumbersToFind[0...3]. Add all the three number's (of each element) possibilities - find the max - keep the position and return the array.
$array = array(
array (
0 => 13,
1 => 15,
2 => 4
),
array (
0 => 20,
1 => 12,
2 => 5
),
array (
0 => 13,
1 => 3,
2 => 15
),
);
$mNumbersToFind = array(13,3,3);
$mFoundArray = find($mNumbersToFind, $array);
echo "mFinalArray : <pre>";
print_r($mFoundArray);
function find($mNumbersToFind, $mArray){
$mPossibilityMax = count($mNumbersToFind);
$mBiggestPossibilityElementPosition = 0;
$mBiggestPossibilityUntilNow = 0;
foreach($mArray as $index => $current){
$maxPossibility = 0;
foreach($current as $subindex => $subcurrent){
$mTempArray[$index][$subindex]['value'] = $subcurrent - $mNumbersToFind[$subindex];
$percentChange = (1 - $mTempArray[$index][$subindex]['value'] / $subcurrent) * 100;
$mTempArray[$index][$subindex]['possibility'] = $percentChange;
$maxPossibility += $percentChange/$mPossibilityMax;
}
$mTempArray[$index]['final_possibility'] = $maxPossibility;
if($maxPossibility > $mBiggestPossibilityUntilNow){
$mBiggestPossibilityUntilNow = $maxPossibility;
$mBiggestPossibilityElementPosition = $index;
}
}
echo "mTempArray : <pre>"; // Remove this - it's just for debug
print_r($mTempArray); // Remove this - it's just for debug
return $mArray[$mBiggestPossibilityElementPosition];
}
Debug Output ($mTempArray) :
mTempArray :
Array
(
[0] => Array
(
[0] => Array
(
[value] => 0
[possibility] => 100
)
[1] => Array
(
[value] => 12
[possibility] => 20
)
[2] => Array
(
[value] => 1
[possibility] => 75
)
[final_possibility] => 65
)
[1] => Array
(
[0] => Array
(
[value] => 7
[possibility] => 65
)
[1] => Array
(
[value] => 9
[possibility] => 25
)
[2] => Array
(
[value] => 2
[possibility] => 60
)
[final_possibility] => 50
)
[2] => Array
(
[0] => Array
(
[value] => 0
[possibility] => 100
)
[1] => Array
(
[value] => 0
[possibility] => 100
)
[2] => Array
(
[value] => 12
[possibility] => 20
)
[final_possibility] => 73.333333333333
)
)
Final Output :
mFinalArray :
Array
(
[0] => 13
[1] => 3
[2] => 15
)
I basically used a concept of proximity (lesser distance total for each array) and returned that. The code was made in a way that can improve very well in so many routines.
PS: I didn't used advanced functions or other things because you are concerned about performance issues. It's most simplest routine I could did in a short period of time.
$array = array(
0 => array (
0 => 35,
1 => 30,
2 => 39
),
1 => array (
0 => 20,
1 => 12,
2 => 5
),
);
$user = array(19,13,3);
function find($referencial, $input){
$totalRef = count($referencial);
if (is_array($referencial)){
for ($i = 0; $i < $totalRef; $i++) {
if (is_array($referencial[$i])){
$totalSubRef = count($referencial[$i]);
$proximity = array();
for ($j = 0; $j < $totalSubRef; $j++) {
$proximity[$i] += abs($referencial[$i][$j] - $input[$j]);
}
if ($i > 0){
if ($maxProximity['distance'] > $proximity[$i]) {
$maxProximity['distance'] = $proximity[$i];
$maxProximity['index'] = $i;
}
} else {
$maxProximity['distance'] = $proximity[$i];
$maxProximity['index'] = $i;
}
}
}
return $maxProximity;
} else {
exit('Unexpected referencial. Must be an array.');
}
}
$found = find($array, $user);
print_r($found);
//Array ( [distance] => 4 [index] => 1 )
print_r($array[$found['index']]);
// Array ( [0] => 20 [1] => 12 [2] => 5 )
The first step is to create an new array with zeros. This is the code:
$amounts = [];
$row = [];
for($a = 0; $a < count($receipts_with_total); $a++){
for($b = 0; $b < count($taxes); $b++){
$row[$b] = 0;
}
$amounts[] = $row;
}
Then, i proceede to fill the array with values. The problem is, for some reason i don't know, it adds some indexs.
The code to fill the array is the next one:
//We calculate all the taxes amounts
for($i = 0; $i < count($receipts_with_total); $i++){
$this_receipt = $receipts_with_total[$i];
//We get all the taxes for each receipt
$taxes = $this_receipt->taxes;
for($j = 0; $j < count($taxes); $j++){
$this_tax = $taxes[$j];
if($this_tax->name == "IVA 21%"){
$amounts[$i][$j] = round((($this_tax->value * $total[$i]) / 100), 2);
}
elseif($this_tax->name == "IVA 10.5%"){
$amounts[$i][$j+1] = round((($this_tax->value * $total[$i]) / 100), 2);
}
else {
$amounts[$i][$j+2] = round((($this_tax->value * $total[$i]) / 100), 2);
}
}
}
And the outputs are:
Creacion
Array ( [0] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [1] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [2] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [3] => Array ( [0] => 0 [1] => 0 [2] => 0 ) )
Modelo
Array ( [0] => Array ( [0] => 0 [1] => 257.46 [2] => 61.3 ) [1] => Array ( [0] => 0 [1] => 40.36 [2] => 9.61 ) [2] => Array ( [0] => 80.73 [1] => 40.36 [2] => 9.61 ) [3] => Array ( [0] => 211.05 [1] => 105.53 [2] => 0 ) )
Lleno
Array ( [0] => Array ( [0] => 0 [1] => 257.46 [2] => 0 [3] => 61.3 ) [1] => Array ( [0] => 0 [1] => 40.37 [2] => 0 [3] => 9.61 ) [2] => Array ( [0] => 80.73 [1] => 0 [2] => 40.37 [4] => 9.61 ) [3] => Array ( [0] => 211.05 [1] => 0 [2] => 105.53 ) )
The first output is the new array with zeros. The second one is an example of as should be the final array with the calculate numbers. The last one is the array really i get.
As you can see, the index in bold represent the errors. For example, the value "61.3" is in fourth position in the first array, instead of third, it would be the correct.
Thanks!
Remove the +1 and +2 from the code.
Just
$amounts[$i][$j]=...
in all cases.
Because if i.e.
$j=2;
it may be become 3 in your code $j+1
My answer just pick that part of your question:
The problem is, for some reason i don't know, it adds some indexs.
I guess you want to show the "IVA 21" always in the 0 index in subarray and "IVA 10.5" always in the 1 index in sub array, and so on...? So you don't have to +1 or +2 in the index...cuz $j has already been increment in the for loop...
Or if you don't know which comes first or maybe you will have more option later, do not use a for loop. Use php foreach and keep +1 manually
$j = 0;
foreach ($taxes as $$this_tax) {
if ($this_tax->name == 'IVA 21%') {
$amounts[$i][$j] = round((($this_tax->value * $total[$i]) / 100), 2);
} elseif ($this_tax->name == 'IVA 10.5%') {
$amounts[$i][$j + 1] = round((($this_tax->value * $total[$i]) / 100), 2);
} else {
$amounts[$i][$j + 2] = round((($this_tax->value * $total[$i]) / 100), 2);
}
//myabe +3 later...
}
Or why not just use a static number like 0,1,2 if you always know the length of $taxes and where you gonna put for your results. You can even create conts like:
define('IVA21', 0); // const IVA21 = 0;
define('IVA105', 1);
// ... more define
//for loop starts
if ($this_tax->name == 'IVA 21%') {
$amounts[$i][IVA21] = round((($this_tax->value * $total[$i]) / 100), 2);
}
I was trying to do a loop to find the weekends between two dates and something was really damn strange.
I have to following code which in my opinion should be working fine.
$weekends = array();
// yyyy-mm-dd
$firstWeekend = '2015-09-04';
$lastWeekend = '2015-12-25';
$firstWeekendTime = strtotime($firstWeekend);
$lastWeekendTime = strtotime($lastWeekend);
$totalWeekends = 0;
for ($i = $firstWeekendTime; $i <= $lastWeekendTime; $i += (7 * 86400)) {
$totalWeekends++;
$weekends[date('Y-m-d', $i)] = array(
date('Y-m-d', $i),
date('Y-m-d', strtotime(date('Y-m-d', $i) . '+ 2 days'))
);
}
Eg. output:
[2015-10-16] => Array
(
[0] => 2015-10-16
[1] => 2015-10-18
)
[2015-10-23] => Array
(
[0] => 2015-10-23
[1] => 2015-10-25
)
[2015-10-29] => Array
(
[0] => 2015-10-29
[1] => 2015-10-31
)
[2015-11-05] => Array
(
[0] => 2015-11-05
[1] => 2015-11-07
)
But this is not correct cause the next weekend after 2015-10-23 is 2015-10-30.
So the output should be this:
[2015-10-16] => Array
(
[0] => 2015-10-16
[1] => 2015-10-18
)
[2015-10-23] => Array
(
[0] => 2015-10-23
[1] => 2015-10-25
)
[2015-10-30] => Array
(
[0] => 2015-10-30
[1] => 2015-11-01
)
[2015-11-05] => Array
(
[0] => 2015-11-05
[1] => 2015-11-07
)
I finally made it work by putting a default timezone and without chaning anything else, it worked.
date_default_timezone_set('America/Los_Angeles');
Can someone understand why does this incrementation fail without the timezone?
And the weirdest thing is that it only fails around ~2015-10-25
I don't want other options to find the weekend... i just want to understand why it acts like this.
Thank you.
Just you have to change the for loop condition with increment value like this
for ($i = $firstWeekendTime; $i <= $lastWeekendTime; $i = strtotime(date('Y-m-d', strtotime(date('Y-m-d', $i) . '+ 7 days'))))
instead of
for ($i = $firstWeekendTime; $i <= $lastWeekendTime; $i += (7 * 86400))
I'm having a lot of difficulty approaching a piece of code in PHP. I have an array of dates and values, for example
dates = (2014-12-01,2014-12-02,2014-12-08,2014-12-09,2014-12-10,2014-12-11)
values = (5,3,7,8,9,2)
You'll note that 12/01 is a Monday, as is 12/08. I'd like to form 4 arrays from these two arrays:
monday = (5,7)
tuesday = (3,8)
wednesday = (0,9)
thursday = (0,2)
You'll note that the arrays are formed by grabbing the values associated with the days of the week. However, in the case that a Wednesday date exists, for example, but the prior Tuesday does not, then the array should have a "0". In other words, the 4 arrays should all be the same length.
Can anyone help me write code in PHP to achieve this? Thanks in advance!
NOTE: So far, I have only determined how to find the day of the week from a date: date('l', strtotime("2014-12-08")); I really can't figure out the general algorithm to solve this.
$dates = array( '2014-12-01','2014-12-02','2014-12-08','2014-12-09',
'2014-12-10','2014-12-11' );
$values = array( 5, 3, 7, 8, 9, 2 );
$date = strtotime(min($dates));
$stop = strtotime(max($dates));
$dates = array_flip($dates);
$out = array();
while($date <= $stop)
{
$tmp = date('Y-m-d', $date);
$out[date('l', $date)][] = isset($dates[$tmp]) && isset($values[$dates[$tmp]]) ?
$values[$dates[$tmp]] : 0;
$date = strtotime('+1 day', $date);
}
print_r($out);
Result:
Array
(
[Monday] => Array
(
[0] => 5
[1] => 7
)
[Tuesday] => Array
(
[0] => 3
[1] => 8
)
[Wednesday] => Array
(
[0] => 0
[1] => 9
)
[Thursday] => Array
(
[0] => 0
[1] => 2
)
[Friday] => Array
(
[0] => 0
)
[Saturday] => Array
(
[0] => 0
)
[Sunday] => Array
(
[0] => 0
)
)
ps: how can I get the an array of all the dates included in the "dates" array associated with only all the Mondays?
Modify the code as, for example:
$tmp = date('Y-m-d', $date);
$exists = isset($dates[$tmp]) && isset($values[$dates[$tmp]]);
$out[date('l', $date)]['numbers'][] = $exists ? $values[$dates[$tmp]] : 0;
if ($exists) $out[date('l', $date)]['dates'][] = $tmp;
$date = strtotime('+1 day', $date);
You'll get an output as (example for monday)
[Monday] => Array
(
[numbers] => Array
(
[0] => 5
[1] => 7
)
[dates] => Array
(
[0] => 2014-12-01
[1] => 2014-12-08
)
)
Might be a better way to get the 0s in there without another loop but I'm headed out:
foreach($dates as $key => $val) {
$day = date('l', strtotime($val));
$result[$day][] = $values[$key];
}
foreach($result as &$val) {
if(count($val) == 1) {
array_unshift($val, 0);
}
}
print_r($result);