Reputation: 345
I am trying to achieve the following array format from posted attributes on a form:
$schedule = array(
'Mon'=>array('09:00','17:00','16:00','15:00','14:00','13:00','12:00','11:00','10:00'),
'Tue'=>array('17:00','16:00','15:00','14:00','13:00','12:00','11:00','10:00','09:00'),
'Wed'=>array('17:00','16:00','15:00','14:00','13:00','12:00','11:00','10:00','09:00'),
'Thu'=>array('12:00','11:00','10:00','09:00'),
'Fri'=>array('09:00'),
'Sat'=>array('17:00','16:00','15:00','14:00','13:00','12:30','11:00','10:00','09:30'),
'Sun'=>array('17:00','16:00','15:00','14:00','13:00','12:00','11:00','10:00','09:00')
);
In my form I have separate Hours and Minutes select boxes to choose the hours and minutes for each day (display wise, they are in tabs).
<div class="controls">
<select name="schedule_hour[0][]" class="input-mini">
<option value="-1">--</option>
<option value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
etc.
</select>
<select name="schedule_minute[0][]" class="input-mini">
<option value="-1">--</option>
<option selected="selected" value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
etc.
</select>
</div>
Where schedule_minute[0][] would be an array of minutes for monday. Tuesday would be schedule_minute[2][] and so on.
As far as I have got I have this:
for($i = 0; $i < 7; $i++) {
print_r(array(date('D', strtotime("Monday +{$i} days")), array_combine($_POST['schedule_hour'][$i], $_POST['schedule_minute'][$i])));
}
Which gets me close, but still not right, the above returns:
Array
(
[0] => Mon
[1] => Array
(
[09] => 00
[10] => 00
[11] => 00
[12] => 00
[13] => 00
[14] => 00
[15] => 00
[16] => 00
[17] => 00
)
)
The idea is to then serialise the array and save it to a database. But I need the structure in the correct format first.
Please help.
Upvotes: 0
Views: 182
Reputation: 1012
Here is the code with all necessary checks:
$result = array();
for ($i = 0; $i < 7; ++$i) {
if (isset($_POST['schedule_hour'][$i], $_POST['schedule_minute'][$i])
&& is_array($_POST['schedule_hour'][$i])
&& is_array($_POST['schedule_minute'][$i])
&& ($count = count($_POST['schedule_hour'][$i]))
&& ($count === count($_POST['schedule_minute'][$i]))
) {
$add = array();
foreach ($_POST['schedule_hour'][$i] as $k => $hour) {
if (!isset($_POST['schedule_minute'][$i][$k])) {
continue;
}
$hour = (int) $hour;
$minute = (int) $_POST['schedule_minute'][$i][$k];
if (($hour >= 0) && ($hour <= 23)
&& ($minute >=0) && ($minute <= 59)
) {
$add[] = str_pad($hour, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minute, 2, '0', STR_PAD_LEFT);
}
}
if ($add) {
$result[date('D', strtotime("Monday +{$i} days"))] = $add;
}
}
}
Assuming we have following values:
$_POST['schedule_hour'] = array(
0 => array(2, 10, 12, 13, 26),
3 => array(11, 12, ),
);
$_POST['schedule_minute'] = array(
0 => array(5, 15, 25, 35, 61),
3 => array(40, 50, ),
);
$result will store:
array(2) {
["Mon"]=>
array(4) {
[0]=>
string(5) "02:05"
[1]=>
string(5) "10:15"
[2]=>
string(5) "12:25"
[3]=>
string(5) "13:35"
}
["Thu"]=>
array(2) {
[0]=>
string(5) "11:40"
[1]=>
string(5) "12:50"
}
}
Upvotes: 1
Reputation: 24645
Instead of array_combine you will need to do something like this...
$times = array();
for($x = 0; $x< count($_POST['schedule_hour'][$i]); $x++){
$times[] = $_POST['schedule_hour'][$i][$x].":".
$_POST['schedule_minute'][$i][$x];
}
So your code should look something like this:
for($i = 0; $i < 7; $i++) {
$times = array();
for($x = 0; $x< count($_POST['schedule_hour'][$i]); $x++){
$times[] = $_POST['schedule_hour'][$i][$x].":".
$_POST['schedule_minute'][$i][$x];
}
print_r(array(date('D', strtotime("Monday +{$i} days"))=>$times);
}
Upvotes: 0