我有一个包含5个输入字段的表单。每个字段都有一个数组名称,因为一次可以创建多个记录。
示例:
<input type="text" name="name[]" />
<input type="text" name="surname[]" />
<input type="text" name="address[]" />
<input type="text" name="phone[]" />
<input type="text" name="age[]" />
我的问题是如何使用php管理多个名称数组?我知道如何使用单个数组(多个复选框已检查案例),但我被困在这里。
$all[] = $_POST["name"];
$all[] = $_POST["surname"];
$all[] = $_POST["address"];
$all[] = $_POST["phone"];
$all[] = $_POST["age"];
???
foreach {....???
答案 0 :(得分:3)
每个输入都有一个索引,并且通过乘以所有值,同一组中的所有字段将具有相同的数组索引:
foreach($_POST['name'] as $num => $one){
echo $_POST['name'][$num]; // or $one
echo $_POST['surname'][$num];
echo $_POST['address'][$num];
echo $_POST['phone'][$num];
echo $_POST['age'][$num];
}
答案 1 :(得分:0)
你可以这样做:
$all = $_POST["name"];
foreach($all as $val) {
//Do whatever with $val
}
答案 2 :(得分:0)
在这种情况下,最好使用多维数组,如下所示:
$all['name'] = $_POST['name'];
$all['surname'] = $_POST['surname'];
$all['address'] = $_POST['address'];
$all['phone'] = $_POST['phone'];
$all['age'] = $_POST['age'];
//1st processing option
foreach ($all as $key => $dataList) {
//some code
foreach ($dataList as $data) {
//some code
}
}
//2nd processing option
foreach ($all['name'] as $data) {
//some code
}
//repeat for each of 'name, surname, address, phone, age'