我搜索了所有论坛,找不到合适的答案我的问题,阵列邮件处理对我来说是个新鲜事。 我有一个html表单,作为数组字段,
<input type="text" name="title[]" />
<input type="text" name="desc[]" />
<input type="text" name="cate[]" />
字段由php生成,我不知道会有多少类似的数组字段。当我输出帖子时,我得到了跟随多维数组。
Array
(
[title] => Array
(
[0] => title 1
[1] => title 2
[2] => title 3
)
[desc] => Array
(
[0] => description 1
[1] => description 2
[2] => description 3
)
[cate] => Array
(
[0] => cat 1
[1] => cat 2
[2] => cat 3
))
现在问题是我想使用php foreach将它们插入到db中。类似......:
foreach($_POST as arraydata)
{
INSERT INTO sometable(title,desc,cata)VALUES(title[0],desc[0],cate[0]);
}
我尝试了所有的技巧,合并,加入,爆炸等,但我认为我的探测器没有确切的解决方案来获得类似下面的内容所以可以很容易地逐个插入它们谢谢:
(
[0] => Array
(
[title] => title 1
[desc] => description 1
[cate] => cat 1
)
[1] => Array
(
[title] => title 2
[desc] => description 2
[cate] => cat 2
)
[2] => Array
(
[title] => title 3
[desc] => description 3
[cate] => cat 3
))
答案 0 :(得分:3)
$title = $_POST['title'];
$desc = $_POST['desc'];
$cate = $_POST['cate'];
for($i=1 ; $i < count($title) ; $i++)
{
INSERT INTO sometable(title,desc,cata)VALUES($title[$i],$desc[$i],$cate[$i]);
}