我得到这样的$ _POST数组:
'UserChildren' =>
array (
'child_name' =>
array (
0 => 'Ram',
1 => 'Shyam',
),
'child_birth_date' =>
array (
0 => '2005-03-30',
1 => '2007-06-15',
),
'child_gender' =>
array (
0 => 'Male',
1 => 'Male',
),
),
)
我想将上面的数组结果转换为保存到数据库:
'UserChildren' =>
array (size=2)
0 =>
array (size=3)
'child_name' => string 'Ram' (length=3)
'child_birth_date' => string '2005-03-30' (length=10)
'child_gender' => string 'Male' (length=4)
1 =>
array (size=3)
'child_name' => string 'Shyam' (length=3)
'child_birth_date' => string '2007-06-15' (length=10)
'child_gender' => string 'Male' (length=4)
Update:
foreach($_POST['UserChildren']['child_name'] as $key => $val) {
$userchild->child_name = $val;
$userchild->child_birth_date = $_POST['UserChildren']['child_birth_date'][$key];
$userchild->child_gender = $_POST['UserChildren']['child_gender'][$key];
}
$userchild->save();
print_r($userchild->getErrors());
答案 0 :(得分:0)
构造像这样的表单输入将提供所需的数组:
<input type="text" name="UserChildren[0][child_name]">
<input type="text" name="UserChildren[0][child_bith_date]">
<input type="text" name="UserChildren[0][child_gender]">
<input type="text" name="UserChildren[1][child_name]">
<input type="text" name="UserChildren[1][child_bith_date]">
<input type="text" name="UserChildren[1][child_gender]">
使用现有数组循环一个数组并使用公共密钥访问其他数组:
foreach($_POST['UserChildren']['child_name'] as $key => $val) {
$name = $val;
$birth = $_POST['UserChildren']['child_birth_date'][$key];
$gender = $_POST['UserChildren']['child_gender'][$key];
}