0

我正在尝试从表单上发布的属性中实现以下数组格式:

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

在我的表格中,我有单独的小时和分钟选择框来选择每天的小时和分钟(明智地显示,它们在选项卡中)。

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

其中 schedule_minute[0][] 将是星期一的分钟数组。星期二是 schedule_minute[2][] 等等。

据我所知,我有这个:

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

这让我很接近,但仍然不对,上面的返回:

Array
(
[0] => Mon
[1] => Array
    (
        [09] => 00
        [10] => 00
        [11] => 00
        [12] => 00
        [13] => 00
        [14] => 00
        [15] => 00
        [16] => 00
        [17] => 00
    )

)

这个想法是然后序列化数组并将其保存到数据库中。但我首先需要正确格式的结构。

请帮忙。

4

2 回答 2

1

这是包含所有必要检查的代码:

$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;
        }
    }
}

假设我们有以下值:

$_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 将存储:

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"
  }
}
于 2013-07-08T14:38:49.137 回答
0

而不是 array_combine 你将需要做这样的事情......

  $times = array();
  for($x = 0; $x< count($_POST['schedule_hour'][$i]); $x++){
      $times[] = $_POST['schedule_hour'][$i][$x].":".
                 $_POST['schedule_minute'][$i][$x];
  }

所以你的代码应该是这样的:

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); 
}
于 2013-07-08T14:28:28.810 回答