Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach loop in php having multiple more than one array [duplicate]

Tags:

loops

foreach

php

I want to loop for more than one array i was able to do for two using this code

$value=array_combine($val1,$val2);

foreach($value as $vl1=> $vl2)

Am now trying to do for more than two using this code but its not working I don't know where am getting it wrong.

  $value=array_combine($val1,$val2,$val3,$val4,$val5,val6);

foreach($value as $vl1=> $vl2,$vl3=> $vl4,$vl5=> $vl6 )

Thanks

Edited

Here's is the complete working code - Thanks to @Barmar With this code, i was able to submit multiple columns and rows into the database.

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");
}
}
}
like image 618
Student Avatar asked May 02 '26 14:05

Student


1 Answers

array_combine() can only take two arrays -- it creates an associative array that uses the first array as the keys, and the second array as the corresponding values. It makes no sense to give it more than 2 arrays, where would those elements go in this result?

Loop over one array, and use its index to access the other arrays.

foreach ($array1 as $index => $val1) {
    $val2 = $array2[$index];
    $val3 = $array3[$index];
    ...
}
like image 76
Barmar Avatar answered May 05 '26 04:05

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!