输入帖子:
$_POST['dateSlot']
$_POST['timeStart']
$_POST['timeEnd']
$_POST['quota']
这些输入帖子将产生以下数组。
Array
(
[dateSlot] => Array
(
[0] => 2018-04-05
[1] => 2018-04-05
[2] => 2018-04-05
)
[timeStart] => Array
(
[0] => 11:06 AM
[1] => 10:06 AM
[2] => 9:06 AM
)
[timeEnd] => Array
(
[0] => 11:06 AM
[1] => 9:06 AM
[2] => 7:06 AM
)
[quota] => Array
(
[0] => 12
[1] => 10
[2] => 10
)
)
我正在尝试将它们与索引键匹配,并形成另一个具有此想法的数组。不太确定是否能获得我想要的价值:
foreach ($_POST['dateSlot'] as $k => $val) {
foreach ($_POST['timeStart'] as $k2 => $val2) {
foreach ($_POST['timeEnd'] as $k3 => $val3) {
foreach ($_POST['quota'] as $k4 => $val4) {
if($k == $k2 && $k == $k3 && $k == $k4){
$timeslots[$k]['date_slot'] = $val;
$timeslots[$k]['time_start'] = $val2;
$timeslots[$k]['time_end'] = $val3;
$timeslots[$k]['event_quota'] = $val4;
}
}
}
}
}
根据该foreach,我收到date_slot
,time_start
,time_end
和event_quota
的错误非法字符串偏移
基于数组中的行,我的目标是重新构建数组,以便将它们组合在一起形成3行。
示例:
Array
(
[0] => Array
(
[date_slot] => 2018-04-05
[time_start] => 11:06 AM
[time_end] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[date_slot] => 2018-04-05
[time_start] => 10:06 AM
[time_end] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[date_slot] => 2018-04-05
[time_start] => 9:06 AM
[time_end] => 7:06 AM
[event_quota] => 10
)
)
答案 0 :(得分:3)
另一种分组此类数据的方法,无需事先知道密钥名称。
这通过使用第一行的数据current( $data )
作为主迭代器来工作,然后通过将外键array_keys( $data )
和内部列值array_column( $data, $column )
与{{1}组合来构建数组它结合了两个键数组和一个值数组,使每一行的最终数组结构都按列名键控。
这绝对依赖于具有相同元素数的每个多维数组。 因此,这不适用于带有复选框输入的表单。此时我建议使用array_combine()
作为您的名称属性,并且无需进行此数组处理。
name="row[0][ColumnName]"
可生产
$data = array(
'Column-1'=>array('Row-1a','Row-2a','Row-3a'),
'Column-2'=>array('Row-1b','Row-2b','Row-3b'),
'Column-3'=>array('Row-1c','Row-2c','Row-3c')
);
$array = array();
foreach( array_keys( current( $data ) ) as $column )
{
$array[] = array_combine( array_keys( $data ), array_column( $data, $column ) );
}
print_r( $array );
答案 1 :(得分:1)
如果您知道所有4个帖子变量中的元素键将始终与一个时间段元素相关联,那么我认为这对您有用:
foreach ($_POST['dateSlot'] as $key => $value) {
$timeslots[$key] = [
'date_slot' => $_POST['dateSlot'][$key],
'time_start' => $_POST['timeStart'][$key],
'time_end' => $_POST['timeEnd'][$key],
'event_quota' => $_POST['quota'][$key],
];
}
print_r($timeslots);
答案 2 :(得分:0)
$dateSlot = $_POST['dateSlot']
$timeStart = $_POST['timeStart']
$timeEnd = $_POST['timeEnd']
$quota = $_POST['quota']
$all = array();
foreach($dateSlot as $key => $date) {
$all[] = array(
"data_slot" => $dateSlot[$key],
"time_start" => $timeStart[$key],
"time_end" => $timeEnd[$key],
"quota" => $quota[$key]
)
}
答案 3 :(得分:0)
<强>输入强>
$array = array(
'dateSlot' => array('2018-04-05','2018-04-05','2018-04-05'),
'timeStart' => array('11:06 AM','10:06 AM','9:06 AM'),
'timeEnd' => array('11:06 AM','9:06 AM','7:06 AM'),
'quota' => array(12,10,10)
);
<强>解决方案强>
$new = array();
for($i=0;$i<count($array['dateSlot']);$i++){
$new[] = array(
'dateSlot' => $array['dateSlot'][$i],
'timeStart' => $array['timeStart'][$i],
'timeEnd' => $array['timeEnd'][$i],
'event_quota' => $array['quota'][$i],
);
}
echo "<pre>";print_r($new);
<强>输出强>
Array
(
[0] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 11:06 AM
[timeEnd] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 10:06 AM
[timeEnd] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 9:06 AM
[timeEnd] => 7:06 AM
[event_quota] => 10
)
)