Selecting a certain item from multi-dimensional array - php

I have an multi-dimensional array called shifts which contains the day (e.g. Monday) and then the shift (e.g. 12:00 - 16:00)
I'm looking to do something like:
$monday = shift from array where day equals Monday
My array currently looks like:
Array ( [0] => Array ( [day] => Saturday [shift] => Day Off! )
[1] => Array ( [day] => Sunday [shift] => Day Off! )
[2] => Array ( [day] => Monday [shift] => 11:00-19:00 )
[3] => Array ( [day] => Tuesday [shift] => 08:00-17:00 )
[4] => Array ( [day] => Wednesday [shift] => 08:00-17:00 )
[5] => Array ( [day] => Thursday [shift] => 16:00-01:00 )
[6] => Array ( [day] => Friday [shift] => 16:00-01:00 ) )
Array is built using this code:
$shifts = array();
$sql = mysql_query("SELECT day, shift FROM ps_shifts WHERE user_id = '$user_id' AND week_begin = '$week_1_begin'");
while($row = mysql_fetch_assoc($sql)) {
$shifts[] = $day;
}

/* to refine on what was said above lets take the the data as such:*/
$data = array(
array ( 'day' => 'Saturday', 'shift' => 'Day Off!' ),
array ( 'day' => 'Sunday', 'shift' => 'Day Off!' ),
array ( 'day' => 'Monday', 'shift' => '11:00-19:00' ),
/* ... */
);
/* we then take that data and transform it, that is flatten it to day=>shift. call it shifts*/
$shifts = array();
$days = array('Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday', 'Friday');
foreach ($data as $i=>$s) {
$shifts[$days[$i]] = $data[$i]['shift'];
}
/* this returns an array like:
$shifts = array (
'Saturday' => 'Day Off!',
'Sunday' => 'Day Off!',
'Monday' => '11:00-19:00',
);
*/
/* then to get the shift for any day is as simple as.*/
$monday_shift = $shifts['Monday'];
print "monday shift is: $monday_shift\n";

You could use the day as the array key.
So:
while($row = mysql_fetch_assoc($sql)) {
$shifts[$day['day']] = $day;
}
Then you could get it by doing this:
$shift_time = $shifts['Monday']['shift'];

Solved based on 'jd182' anwser, however the provided code didn't work so I modified to this:
$shifts = array();
$sql = mysql_query("SELECT day, shift FROM ps_shifts WHERE user_id = '$user_id' AND week_begin = '$week_1_begin'");
while($row = mysql_fetch_assoc($sql)) {
$shifts[$row['day']] = $row['shift'];
}
echo $shifts['Monday'];

Related

Filter multidimensional array and push in new one

I am trying to build a class schedule that takes a .csv with data for the whole semester. I have this multidimensional array, which contains the date, class, instructor, room, and notes for every day of the semester:
Array (
Array (
[0] => 1
[1] => 2019-04-02
[2] => Study Skills
[3] => 371
[4] => Mr Teacher
[5] => 0
[6] =>
)
Array ( ... )
...)
I managed to display the schedule for the whole semester in one table. However, ideally, I would filter out the data for each month and then push in into a new multidimensional array for the whole semester.
I was able to filter out individual months by comparing it a to hardcoded string but I obviously don't want to do that for the whole semester:
$may = "05";
$classMay = [];
foreach ($days as $day){
if (strrpos($day[1], $may, -5)){
array_push($classMay, $day);
}
};
Is there a more efficient solution to this?
Any insight is much appreciated!
Just get the month name from the date of each one and append the day data onto an array indexed by the month:
foreach($days as $day) {
$month = date('F', strtotime($day[1]));
$result[$month][] = $day;
}
Now you should have an array that looks similar to this:
Array
(
[January] => Array
(
[0] => Array
(
[0] => 1
[1] => 2019-01-01
//etc
)
[1] => Array
(
[0] => 3
[1] => 2019-01-02
//etc
)
)
[February] => Array
(
[0] => Array
(
[0] => 10
[1] => 2019-02-01
//etc
)
[1] => Array
(
[0] => 30
[1] => 2019-02-02
//etc
)
)
)
For fun you could loop 12 months and filter on the month:
foreach(range(1, 12) as $m) {
$month = date('F', strtotime($m));
$result[$month] = array_filter($days, function($v) use($month) {
return $month == date('F', strtotime($v[1]));
});
}

How to exclude weekends between two date ranges

I have a FromDate and ToDate and days interval between them.
Now i want to get all date excluding weekends
If there is any weekends between my FromDate and ToDate,then this date will be increment by 2 days saturday and sunday.
In below example, i have calculated all date between 7/27/2018 and 8/27/2018 with 5 days interval between them,
public function testdate(){
$date_from = new \DateTime("7/27/2018");
$date_to = new \DateTime("8/27/2018");
$interval = new \DateInterval("P5D");
$dates = new \DatePeriod($date_from, $interval, $date_to);
$out = array();
if (!empty($dates)) {
foreach($dates as $dt) {
$out[] = array(
'month_year' => $dt->format('d/m/Y')
);
}
}
'<pre>';
//$out = array_reverse($out);
echo print_r($out);
'</pre>';
exit;
}
Expected output
Array
(
[0] => Array
(
[month_year] => 27/07/2018
)
[1] => Array
(
[month_year] => 02/08/2018
)
[2] => Array
(
[month_year] => 08/08/2018
)
[3] => Array
(
[month_year] => 14/08/2018
)
[4] => Array
(
[month_year] => 20/08/2018
)
[5] => Array
(
[month_year] => 27/08/2018
)
)
In above example, after 20/08/2018, next date will be 26/08/2018 but 26/08/2018 is sunday, so we exclude this sunday by 1 day and next date is now
27/08/2018.
If any occurence of date after calculating interval in between saturday and sunday then skip this weekend by 1 or 2days.
$year = date('Y');
$month = date('n');
$weekend_days = array_filter($allDays[$year][$month], function($d) {
return (date('N', strtotime(date("Y-m-$d"))) >= 6);
});
$allDays[$year][$month] = array_diff($allDays[$year][$month], $weekend_days);
print_r($allDays);

How to convert multi-dimensional array of PHP data to an HTML table?

I'm trying to display a formatted HTML timetable using PHP.
I would like to output data from a multi-dimensional array to an HTML table that's formatted with a total of 8 columns (a column for each day Mon-Sun plus a column on the left showing the start times for each session).
The amount of rows depends on how many sessions there are for each day.
My solution works to a degree and you can see the output in the following image but you can also see that an extra row is generated for some reason. It's always just one extra row no matter the amount of sessions in a day.
The data is represented like this:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => A Monday Session
[start_time] => 10:00
)
[1] => Array
(
[id] => 5
[name] => Another Monday Session
[start_time] => 11:00
)
[2] => Array
(
[id] => 6
[name] => Yet Another Monday Session
[start_time] => 12:00
)
)
[1] => Array
(
[0] => Array
(
)
)
[2] => Array
(
[0] => Array
(
[id] => 8
[name] => A Wednesday Session
[start_time] => 14:30
)
)
[3] => Array
(
[0] => Array
(
[id] => 3
[name] => A Thursday Session
[start_time] => 09:00
)
)
[4] => Array
(
[0] => Array
(
)
)
[5] => Array
(
[0] => Array
(
)
)
[6] => Array
(
[0] => Array
(
[id] => 4
[name] => A Sunday Session
[start_time] => 13:00
)
)
)
As you can see the main keys represent the days of the week. 0 = Monday, 1 = Tuesday etc.
Each day then has a list of sessions. In this example Monday has 3 sessions, and Wed,Thu,Sun each also have one.
Notice that they all have different starting times. The moment a session is introduced to the data with a duplicate start time, then extra rows are also created instead of sharing the same row. See output with the Thursday session changed to 10:00 to be the same as Monday:
And here is my buggy solution. I've marked with a comment the line where I'm going wrong.
// $sessionRows is the array of arrays containing the data above.
// Grabs array of session times from $sessionRows that has been sorted and duplicates removed.
// Current values: Array ( [0] => 09:00 [1] => 10:00 [2] => 11:00 [3] => 12:00 [4] => 13:00 [5] => 14:30 )
$sessionTimes = $this->getSessionTimes($days);
$numOfRows = count($sessionTimes);
$numOfCols = $dayIdx;
// Create grid with correct dimensions and rows represented by the session times
$grid = array();
for($i=0;$i<count($sessionTimes);$i++) {
$row = array();
for($j=0;$j<$numOfCols;$j++) {
$row[] = '<td></td>';
}
$grid[$sessionTimes[$i]] = $row;
}
// Populate grid with session info added to correct coordinates.
for($i=0;$i<$numOfCols;$i++) {
echo count($sessionRows[$i]);
for($j=0;$j<count($sessionRows);$j++) {
$grid[$sessionRows[$i][$j]['start_time']][$i] = $sessionRows[$i][$j];
}
}
$rows='';
$idx = 0;
foreach($grid as $rowArray) {
$rows .= '<tr>';
/*** This lines is the problem! It adds the session time as the first column. ***/
$rows .= '<td class="time-col">'.$sessionTimes[$idx].'</td>';
for($i=0;$i<count($rowArray);$i++) {
if(!empty($rowArray[$i]['name'])){
$rows .= '<td>'.$rowArray[$i]['name'].'<br>'.$rowArray[$i]['start_time'].'</td>';
} else {
$rows .= '<td> - </td>';
}
}
$rows .= '</tr>';
$idx++;
}
return $rows;
The problem was the $sessionTimes array was having duplicates removed by using the array_unique() function.
This function was preserving the index of the removed values. So the extra rows were being created without value.
Changing this to array_values(array_unique($array)) allowed regenerated keys and fixed the issue.
Thanks to anyone who had a look at it, wish I'd realised this earlier!
Here is an alternative solution that uses less loops.
What it does is it loops through the times, then loops through each day to determine whether the session start_time matches, if it does, add it to the output table.
<?php
$sessionTimes = ['09:00', '10:00', '11:00', '12:00', '13:00', '14:30'];
$schedule = [
[
[
'id' => 1,
'name' => 'A Monday Session',
'start_time' => '10:00'
],
[
'id' => 5,
'name' => 'Another Monday Session',
'start_time' => '11:00'
],
[
'id' => 6,
'name' => 'Yet Another Monday Session',
'start_time' => '12:00'
],
],
[
[]
],
[
[
'id' => 8,
'name' => 'A Wednesday Session',
'start_time' => '14:30'
]
],
[
[
'id' => 3,
'name' => 'A Thursday Session',
'start_time' => '09:00'
]
],
[
[]
],
[
[]
],
[
[
'id' => 4,
'name' => 'A Sunday Session',
'start_time' => '13:00'
]
]
];
$output = '<table><thead><tr><th></th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th></thead><tbody>';
foreach ($sessionTimes as $sessionTime) {
$output .= '<tr><td>'.$sessionTime.'</td>';
$day = 0;
for ($i = 0; $i < count($schedule); $i++) {
if ($i == $day) {
$sessionAtTime = '<td>-</td>';
foreach ($schedule[$day] as $session) {
if (!empty($session) && $session['start_time'] == $sessionTime) {
$sessionAtTime = '<td><b>' . $session['name'] . '</b><br>Start: ' . $sessionTime.'</td>';
}
}
$output .= $sessionAtTime;
$day++;
}
}
$output .= '</tr>';
}
$output .= '</tbody></table>';
echo $output;

Manipulating arrays of dates in PHP

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);

Fill in missing elements of an array of date objects in PHP [duplicate]

This question already has answers here:
Push rows with default values into an array containing dates so there are no gaps between dates
(4 answers)
Closed 5 months ago.
I have an array of objects in php which looks like:
Array
(
[0] => Array
(
[day] => 1/23/2013
[executions] => 1
)
[1] => Array
(
[day] => 1/24/2013
[executions] => 1
)
[2] => Array
(
[day] => 1/27/2013
[executions] => 10
)
[3] => Array
(
[day] => 1/29/2013
[executions] => 1
)
[4] => Array
(
[day] => 1/30/2013
[executions] => 3
)
[5] => Array
(
[day] => 2/8/2013
[executions] => 1
)
[6] => Array
(
[day] => 2/11/2013
[executions] => 3
)
)
I am building a graph of this data, and basically it represents the last 30 days. The problem is I don't get the missing days, i.e. when there was no executions from the query. I am looking to fill in these missing days with PHP, simple set the day to the correct date, and executions to 0. Thus the result array should contain 30 elements, assuming start is 1/18/2013 and end is today 2/17/2013.
Any idea of the best algorithm to accomplish this in PHP?
Something like:
$start = '1/18/2013';
$end = '2/17/2013';
$range = new DatePeriod(
DateTime::createFromFormat('m/d/Y', $start),
new DateInterval('P1D'),
DateTime::createFromFormat('m/d/Y', $end));
$filler = array();
foreach($range as $date)
$filler[] = array(
'day' => $date->format('m/d/Y'),
'execution' => 0,
};
$array += $filler;
Loop through each date using DateTime:
$start = new DateTime('2013-01-18');
$end = new DateTime('2013-02-17');
while ($start <= $end)
{
$current_date = $start->format('m/d/Y');
// Right here look in your array and see if that date exists
// and do whatever you need to do if it does/does not
$start->modify("+1 day");
}
You may use this:
$startDate = new DateTime ( "-30 days" );
$dateItter = new DatePeriod (
$startDate,
new DateInterval ('P1D'),
30
);
$original = array (
array (
'days' => '02/16/2013',
'executions' => 5
)
);
$result = array ();
foreach ( $dateItter as $date )
{
$executions = 0;
foreach ( $original as $item ) {
if ( $item['days'] == $date->format ( 'm/d/Y' ) )
$executions = $item['executions'];
}
$result[] = array (
"day" => $date->format ( 'm/d/Y' ),
"executions" => $executions
);
}
var_dump ( $result );
It is slow for large amount of data but for 30 items will be ok!

Categories