我想循环遍历多个数组,我可以使用此代码循环遍历两个数组
$value=array_combine($val1,$val2);
foreach($value as $vl1=> $vl2)
我现在尝试使用此代码执行两个以上的操作,但它不起作用 我不知道哪里出错了。
$value=array_combine($val1,$val2,$val3,$val4,$val5,val6);
foreach($value as $vl1=> $vl2,$vl3=> $vl4,$vl5=> $vl6 )
谢谢
已编辑
这是完整的工作代码 - 感谢@Barmar 使用此代码,我能够将多个列和行提交到数据库中。
if(isset($_POST['submit'])){
$head = $_POST['head'];
$surname = $_POST['surname'];
$othernames = $_POST['othernames'];
$affiliate_email = $_POST['affiliate_email'];
$affiliation = $_POST['affiliation'];
$phone_no = $_POST['phone_no'];
$value=array_combine($surname,$affiliate_email,$othernames,
$head,$affiliation,$phone_no);
foreach ($surname as $index => $sur) {
$affmail = $affiliate_email[$index];
$names = $othernames[$index];
$hd = $head[$index];
$affil = $affiliation[$index];
$phone = $phone_no[$index];
$page3="INSERT INTO tbl_name
(affiliate_email,surname,othernames,affiliation,phone_no,head) VALUES
'$affmail','$sur','$names','$affil','$phone','$hd')";
if(mysqli_query($db->connection, $page3)) {
header("location:page4?code=$app_code");
}
}
}
最佳答案
array_combine()
只能采用两个数组 - 它创建一个关联数组,使用第一个数组作为键,使用第二个数组作为相应的值。给它超过 2 个数组是没有意义的,这些元素会在这个结果中去哪里?
循环一个数组,并使用其索引访问其他数组。
foreach ($array1 as $index => $val1) {
$val2 = $array2[$index];
$val3 = $array3[$index];
...
}
关于php - php中的foreach循环有多个多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49660675/