我想把post数组放入数组中。我有下面的表单(这个表单在foreach循环中),它将发布如下数组数据:
<input type="hidden" name="cl[]" value="">
<input type="hidden" name="cd[]" value="">
当在firebug中查看时,post数组包含:
cl[] 4
cl[] 4
cd[] John
cd[] Shaw
我想把这些值放在下面这样的一个单一值中:
$allData = array(
array(
'cl' => 4,
'cd' => 'John',
),
array(
'cl' => 4,
'cd' => 'Shaw',
)
);
请帮忙。
为了方便访问,命名阵列(可选)
<input type="text" name="data[cl][]" />
<input type="text" name="data[cd][]" />
<input type="text" name="data[cl][]" />
<input type="text" name="data[cd][]" />
双前臂循环:
foreach($_POST['data'] as $key => $row) {
foreach($row as $subkey => $values) {
$array[$subkey][$key] = $values;
}
}
print_r($array); ?>
给你:
Array
(
[0] => Array
(
[cl] => val1
[cd] => val2
)
[1] => Array
(
[cl] => val3
[cd] => val4
)
)
$cl[] = 4;
$cl[] = 4;
$cd[] = 'John';
$cd[] ='Shaw';
if (!empty($cl) && !empty($cd)) {
$count = count($cl);
for ($i = 0; $i < $count; $i++) {
$newArr[] = array('c1' => $cl[$i], 'cd' => $cd[$i]);
}
}
print_r($newArr);