Pad missing dates in array with value previous day - php

I have an array generated from a database showing continuous date ranges for products in stock.
The problem I'm having is that when there are missing dates the date range gets broken in 2 parts as shown in a segment of the array below where there are no records for dec 31.
I can tell it is a missing date because the elements [0],[1],[2] in the row before the missing date are equal to the elements [0],[1],[2] in the row after the missing date. Element[3] is the number of days in the [4], [5] date range.
[20] => Array ( [0] => 745637 [1] => 24759 [2] => 6.00 [3] => 23 [4] => 2016-12-08 [5] => 2016-12-30 )
[21] => Array ( [0] => 745637 [1] => 24759 [2] => 6.00 [3] => 2 [4] => 2017-01-01 [5] => 2017-01-02 )
[22] => Array ( [0] => 745637 [1] => 26400 [2] => 5.70 [3] => 23 [4] => 2016-12-08 [5] => 2016-12-30 )
[23] => Array ( [0] => 745637 [1] => 26400 [2] => 5.70 [3] => 2 [4] => 2017-01-01 [5] => 2017-01-02 )
[24] => Array ( [0] => 745637 [1] => 29882 [2] => 7.00 [3] => 23 [4] => 2016-12-08 [5] => 2016-12-30 )
[25] => Array ( [0] => 745637 [1] => 29882 [2] => 7.00 [3] => 2 [4] => 2017-01-01 [5] => 2017-01-02 )
So what I would like to do is merge these rows IF there is only a 1 day gap AND elements [0],[1],[2] in the row before the missing date are equal to the elements [0],[1],[2] after the missing date.
[20] => Array ( [0] => 745637 [1] => 24759 [2] => 6.00 [3] => 23 [4] => 2016-12-08 [5] => 2016-12-30 )
[21] => Array ( [0] => 745637 [1] => 24759 [2] => 6.00 [3] => 2 [4] => 2017-01-01 [5] => 2017-01-02 )
would become:
[20] => Array ( [0] => 745637 [1] => 24759 [2] => 6.00 [3] => 26 [4] => 2016-12-08 [5] => 2017-01-02 )
UPDATE:
This is what I came up with, and it actually works but I'm wondering if there is a cleaner way of doing this
foreach ($data as $rows) {
$current = $rows[0].$rows[1].$rows[2];
$cur_days = $rows[3];
$cur_begin = $rows[4];
$cur_end = $rows[5];
// check if we have a 1-day gap in the date range
$comp_date = date_add(date_create($prev_end), date_interval_create_from_date_string('2 days'));
$comp_date = date_format($comp_date, 'Y-m-d');
if (($current == $previous) && ($comp_date == $cur_begin)) {
$data[$i-1][5] = $cur_end; // update [5] 'end date range'
// calculate new range in days
$date_prev_begin = date_create("$prev_begin");
$date_cur_end = date_create("$cur_end");
$interval = date_diff($date_prev_begin, $date_cur_end);
$new_days=$interval->format('%a');
$data[$i-1][3] = $new_days+1; // update [3] 'days'
unset($data[$i]); // remove row
}
// store current row to use for comparison in next iteration
$previous = $current;
$prev_days = $cur_days;
$prev_begin = $cur_begin;
$prev_end = $cur_end;
$i++;
}
$data=array_values($data); // rearrange array

Not sure if it is more efficient but it contains a few less lines of code:
$new_data = array();
foreach($data as $row) {
$cur_begin = new DateTime($row[4]);
$new_row = $row;
$days = 0;
$last_index = !empty($new_data) ? count($new_data)-1 : -1;
$prev_row = $last_index >= 0 ? $new_data[$last_index] : null;
if(!empty($prev_row)) {
$prev_end = new DateTime($prev_row[5]);
$interval = $prev_end->diff($cur_begin);
$days = $interval->days;
$isdiff = $row[0].$row[1].$row[2] != $prev_row[0].$prev_row[1].$prev_row[2];
}
if(!empty($prev_row) && $days > 0 && !$isdiff) {
$new_data[$last_index][3] += $days+1;
$new_data[$last_index][5] = $row[5];
} else {
$new_data[] = $row;
}
}
var_dump($new_data);
Hope it helps.

Related

Loop through an array and group prices by month, Morris chart

I want to group results by month, to be precise Sum of total prices for each month, but i don't understand how i can divide months, then Sum price for each month.
This is required for morris chart like this
$data2[] = array(
'y' => $month,
'a' => price,
'b' => ''
);
I was using before query to get this results and was a easy way to do it, but now with code is different story.
Any help with explanation of how each segment of code is work will be so nice, so i can avoid problems like this one in future.
I need something like this :
$month = [May, June];
$price = [Sum of all prices for May, Sum of all prices for June];
This is a code :
$monthsArray = array();
// get all dates in array
foreach ($array as $key) {
// convert dates to short format, month-year -> Jun-18
$monthsArray[] = date('M-y', strtotime($key['time']));
}
$months = array();
foreach ($monthsArray as $date) {
$mon = substr($date, 0, 6);
if (!in_array($mon, $months)) array_push($months, $mon);
}
foreach ($months as $m) {
// final array for chart
$data2[] = array('y'=>$m, 'a'=>'', 'b'=>'');
}
$morris = json_encode($data2);
This is array i have
Array
(
[0] => Array
(
[price] => -1835.25
[time] => 2018-05-29 16:53:38
)
[1] => Array
(
[price] => -1743.52
[time] => 2018-05-29 16:53:39
)
[2] => Array
(
[price] => -4445.55
[time] => 2018-05-31 13:21:04
)
[3] => Array
(
[price] => -34647.04
[time] => 2018-05-31 18:29:43
)
[4] => Array
(
[price] => 16888.41
[time] => 2018-06-01 15:05:24
)
[5] => Array
(
[price] => 14369.05
[time] => 2018-06-07 14:44:21
)
[6] => Array
(
[price] => -49579.69
[time] => 2018-06-11 09:14:42
)
[7] => Array
(
[price] => -33300.94
[time] => 2018-06-08 23:50:29
)
[8] => Array
(
[price] => 4413.21
[time] => 2018-06-12 07:15:52
)
[9] => Array
(
[price] => 2724.69
[time] => 2018-06-12 07:15:46
)
[10] => Array
(
[price] => 10224.03
[time] => 2018-06-08 14:00:13
)
[11] => Array
(
[price] => -797.92
[time] => 2018-06-08 13:54:08
)
[12] => Array
(
[price] => -25157.34
[time] => 2018-06-11 11:31:31
)
[13] => Array
(
[price] => 2701.6
[time] => 2018-06-11 14:32:08
)
[14] => Array
(
[price] => 2038.92
[time] => 2018-06-12 07:15:48
)
[15] => Array
(
[price] => -10541.58
[time] => 2018-06-15 10:35:58
)
)
EDIT:
I manage to group months as you can see in updated code and now i am getting only 2 results as i should be, now its just a issue with SUM of data for each month.
This is what i am getting in console
0: {y: "May-18", a: "", b: ""}
1: {y: "Jun-18", a: "", b: ""}

Creating a multidimensional array using an array and loop through mysql table

I have a table with the following structure. There are currently 9 students enrolled in 3 classes (classes 01, 16 and 52), with 3 enrollments in each class.
studentcode | classcode
-------------------------
D3kcIvyp | 01
DdIhBc | 01
2vx0KV | 01
9p1PxWCV | 52
RnmfH1WE | 52
MSTBEv | 52
Yp4J3o | 16
fWQ5Oo | 16
6sxw3u | 16
This is the array with all current classes:
$classes_array = array ( [0] => 01 [1] => 52 [2] => 16 )
Using this array I would like to go through the mysql table and create a multidimensional array with the classcodes used as first level keys:
Array
(
[01] => Array
(
[0] => D3kcIvyp
[1] => DdIhBc
[2] => 2vx0KV
)
[52] => Array
(
[0] => 9p1PxWCV
[1] => RnmfH1WE
[2] => MSTBEv
)
[16] => Array
(
[0] => Yp4J3o
[1] => fWQ5Oo
[2] => 6sxw3u
)
)
foreach ($classes_array as $key => $value)
{
$sql = "SELECT studentcode FROM table WHERE classcode = '$classes_array[$key]'";
$result = mysql_query($sql) or die();
while($data = mysql_fetch_array($result))
{
$studentcodes_array[] = $data['studentcode'];
}
foreach ($studentcodes_array as $studentcodeskey => $studentcodesvalue)
{
$multid_array[$value][] = $studentcodesvalue;
}
}
Using the code above, unfortunately I don't get the array I would like to get. I get this instead:
Array
(
[01] => Array
(
[0] => D3kcIvyp
[1] => DdIhBc
[2] => 2vx0KV
)
[52] => Array
(
[0] => D3kcIvyp
[1] => DdIhBc
[2] => 2vx0KV
[3] => 9p1PxWCV
[4] => RnmfH1WE
[5] => MSTBEv
)
[16] => Array
(
[0] => D3kcIvyp
[1] => DdIhBc
[2] => 2vx0KV
[3] => 9p1PxWCV
[4] => RnmfH1WE
[5] => MSTBEv
[6] => Yp4J3o
[7] => fWQ5Oo
[8] => 6sxw3u
)
)
What is wrong with my code? Can anybody help me with this?
You never initialise $studentcodes_array so it is remembering all the values from your previous loops.
Try adding $studentcodes_array = array(); before your while.

php multidimensional array path segment combination loop

I am trying to figure a way to get this to work. But I have a hard time thinking out the logics.
I have this array:
Array
(
[0] => Array
(
[0] => news
[1] => {section}
[2] => {slug}
[3] => {*}
)
[1] => Array
(
[0] => {id}
[1] => {*}
)
[2] => Array
(
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
That I need to convert to this result:
0 news/{id}/{date}
1 news/{id}/25-07-1982
2 news/{id}/{section}
3 news/{id}/{slug}
4 news/{id}/{*}
5 news/{*}/{date}
6 news/{*}/25-07-1982
7 news/{*}/{section}
8 news/{*}/{slug}
9 news/{*}/{*}
10 {section}/{id}/{date}
11 {section}/{id}/25-07-1982
12 {section}/{id}/{section}
13 {section}/{id}/{slug}
14 {section}/{id}/{*}
15 {section}/{*}/{date}
16 {section}/{*}/25-07-1982
17 {section}/{*}/{section}
18 {section}/{*}/{slug}
19 {section}/{*}/{*}
20 {slug}/{id}/{date}
21 {slug}/{id}/25-07-1982
22 {slug}/{id}/{section}
23 {slug}/{id}/{slug}
24 {slug}/{id}/{*}
25 {slug}/{*}/{date}
26 {slug}/{*}/25-07-1982
27 {slug}/{*}/{section}
28 {slug}/{*}/{slug}
29 {slug}/{*}/{*}
30 {*}/{id}/{date}
31 {*}/{id}/25-07-1982
32 {*}/{id}/{section}
33 {*}/{id}/{slug}
34 {*}/{id}/{*}
35 {*}/{*}/{date}
36 {*}/{*}/25-07-1982
37 {*}/{*}/{section}
38 {*}/{*}/{slug}
39 {*}/{*}/{*}
The input array could contain more than three keys, so the solution I'm looking for should be dynamic. And the result should have the same order as the result shown above.
Does someone know how to do this in a efficient way? Can someone give me a push in the right direction? Thanks a lot! :)
Sth like this
foreach ($array[0] as $val0 )
foreach ($array[1] as $val1 )
foreach ($array[2] as $val2 )
$newArray[] = "$val0/$val1/$val2";
EDIT: for variable array length
function recursive($array , $length = 0){
$retval =array();
if($length < count($array) -1){
foreach ($array[$length] as $val0 )
foreach (recursive($array, $length+1) as $val1)
$retval[] = "$val0/$val1";
}
else
{
foreach ($array[$length] as $val0 )
$retval[] = "$val0";
}
return $retval;
}
print_r(recursive($array));
Just because I like writing functions that mis/manage PHP arrays, I put this together, mainly because I was pretty sure you could avoid recursion — because the structure itself isn't recursive. (My head seems to think that is a rule, I'm sure someone somewhere can prove it wrong).
foreach ( array_reverse($array) as $sub ) {
if ( isset($rem) ) {
$ret = array();
foreach ( $sub as $itm ) {
foreach ( $rem as $val ) { $ret[] = "$itm/$val"; }
}
$rem = $ret;
}
else {
$rem = $sub;
}
}
The output found in $rem is as follows:
Array (
[0] => news/{id}/{date}
[1] => news/{id}/25-07-1982
[2] => news/{id}/{section}
[3] => news/{id}/{slug}
[4] => news/{id}/{*}
[5] => news/{*}/{date}
[6] => news/{*}/25-07-1982
[7] => news/{*}/{section}
[8] => news/{*}/{slug}
[9] => news/{*}/{*}
[10] => {section}/{id}/{date}
[11] => {section}/{id}/25-07-1982
[12] => {section}/{id}/{section}
[13] => {section}/{id}/{slug}
[14] => {section}/{id}/{*}
[15] => {section}/{*}/{date}
[16] => {section}/{*}/25-07-1982
[17] => {section}/{*}/{section}
[18] => {section}/{*}/{slug}
[19] => {section}/{*}/{*}
[20] => {slug}/{id}/{date}
[21] => {slug}/{id}/25-07-1982
[22] => {slug}/{id}/{section}
[23] => {slug}/{id}/{slug}
[24] => {slug}/{id}/{*}
[25] => {slug}/{*}/{date}
[26] => {slug}/{*}/25-07-1982
[27] => {slug}/{*}/{section}
[28] => {slug}/{*}/{slug}
[29] => {slug}/{*}/{*}
[30] => {*}/{id}/{date}
[31] => {*}/{id}/25-07-1982
[32] => {*}/{id}/{section}
[33] => {*}/{id}/{slug}
[34] => {*}/{id}/{*}
[35] => {*}/{*}/{date}
[36] => {*}/{*}/25-07-1982
[37] => {*}/{*}/{section}
[38] => {*}/{*}/{slug}
[39] => {*}/{*}/{*}
)
Also, for those that like their arrays multidimensional, this might come in handy (although I'd hate to think what the overheads are for such a code golfed version). Just to be clear, this second example doesn't create the string list as requested by the OP, but a hierarchical array structure instead.
foreach ( array_reverse($array) as $sub ) {
$rem = isset($rem)
? array_combine($sub, array_fill(0, count($sub), $rem))
: $sub
;
}
This generates (again in $rem):
Array (
[news] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
[{*}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
[{section}] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
... and so on
Now if only PHP had a join_recursive that included keys.
(it would be almost pointless, save for helping with the above).

2 arrays count how many times id = date

Ive sat with this for a while now, and its getting late.
Im trying to get a top 3 of most sales from last month, and i need to count how many times a id from array 1 is equal to array 2 last month(6 = last atm.) like id 4 = 2, id 7 = 3
It might not be the perfect solution, but im just trying to break it down by my self, so later on 'maybe' problems, will i take care of when i hit the wall,
so please, if anyone can help me here, ill be greatfull.
UPDATE
- I will add the result im looking for here: (sorry i didnt before, it makes it alot easier :-)
- The result below, is because i want the count from 2014-06-01 and up to the last day of that month monly, on array[0][1] under this array, only 6-7-8 is not from 2014-06
Hope it makes a bit more sense now ^^
Array
(
[0] => Array
(
[0] => Array
(
[4] => 2
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
)
I hope that what I understood is what you're really asking for. let's say your array definition is :
$arr = Array
(
0 => Array
(
0 => Array
(
0 => 4,
1 => 4,
2 => 7,
3 => 1,
4 => 7,
5 => 7,
6 => 3,
7 => 3,
8 => 4,
9 => 9,
10 => 12,
11 => 2,
12 => 13,
13 => 1
),
1 => Array
(
0 => "2014-06-18",
1 => "2014-06-10",
2 => "2014-06-05",
3 => "2014-06-05",
4 => "2014-06-12",
5 => "2014-06-11",
6 => "2013-12-12",
7 => "2014-07-23",
8 => "2014-05-13",
9 => "2014-06-01",
10 => "2014-06-12",
11 => "2014-06-04",
12 => "2014-06-04",
13 => "2014-06-11"
)
)
);
If you need to test if the date is lower than 6 month and then put their id, sales and date you need to use this code
$result = [];
$index=0;
foreach ($arr[0][0] as $key => $value)
{
$date1 = new DateTime($arr[0][1][$key]);
$date2 = new DateTime();
$diff = $date1->diff($date2);
$diff = ($diff->format('%y') * 12) + $diff->format('%m');
if($diff<=6)
{
$result[$index]['id'] = $key;
$result[$index]['sales'] = $value;
$result[$index]['date'] = $arr[0][1][$key];
$index++;
}
}
var_dump($result);
array_count_values() will give you the number of times each value appears in array 1.
$count = array_count_values($array[0][0]);
Result:
Array
(
[4] => 3
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
Then you can use a loop to combine with array 2:
$result = array();
foreach($count as $key=>$val) {
$result[$array[0][1][$key]] = $val;
}
Result:
Array
(
[2014-06-12] => 3
[2014-07-23] => 3
[2014-06-10] => 2
[2014-06-05] => 1
[2014-06-01] => 1
[2014-06-04] => 1
[2014-06-11] => 1
)
See demo

PHP Remove subarray from main array if specific value range is found

Lets say I have the following array:
[9] => Array
(
[0] => Bob Smith
[1] => bobsmith#gmail.com
[2] => Helsinki
[3] => 10
[4] => 34
[5] => 2014-05-12
)
[10] => Array
(
[0] => John Smith
[1] => johnsmith#domain.com
[2] => some values
[3] => 9
[4] => 67
[5] => 2014-05-15
)
[11] => Array
(
[0] => Clarke Kent
[1] => ckent#superman.com
[2] => Crystal
[3] => 9
[4] => 89
[5] => 2014-05-16
)
What if i want to remove a subarray when the date falls outside a specific range. So if i say wanted to find data where the date is between 2014-05-14 and 2014-05-28. Then the (new) array would print the following:
[10] => Array
(
[0] => John Smith
[1] => johnsmith#domain.com
[2] => some values
[3] => 9
[4] => 67
[5] => 2014-05-15
)
[11] => Array
(
[0] => Clarke Kent
[1] => ckent#superman.com
[2] => Crystal
[3] => 9
[4] => 89
[5] => 2014-05-16
)
I thought something like the following:
foreach ($array as $row) {
if($row[5] >= '2014-05-14' && $row[5] <= '2014-05-14') {
// Do Something // e.g. unset subarray?
}
}
OR should i approach it differently and iterate through the array and if a subarray matches my criteria create a new array. The result being an array with subarrays that contain dates that meet my date range. I hope this makes sense, i am trying to find the most efficient way to do this.
Thanks
For the use case described, I would personally use array_filter:
$start_date = DateTime::createFromFormat('Y-m-d', '2014-05-14');
$end_date = DateTime::createFromFormat('Y-m-d', '2014-06-14');
$filtered_array = array_filter($array, function($row) use ($start_date, $end_date) {
$date = DateTime::createFromFormat('Y-m-d', $row[5]);
return $date >= $start_date && $date <= $end_date;
});

Categories