3

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"); } } } 
4
  • What is the goal? Arrays have equal length? Commented Apr 4, 2018 at 21:25
  • 1
    thats an invalid use of foreach() but without knowing what the input arrays look like or the desired output, thats all i can say Commented Apr 4, 2018 at 21:26
  • are you looking for array_merge instead of array_combine ?? Commented Apr 4, 2018 at 21:34
  • Are the arrays correlated, meaning item1 in array1 is related to item1 in array2 and is related to item1 in array3 ... and so on. All item1's are related. Commented Apr 4, 2018 at 22:03

4 Answers 4

3

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]; ... } 
Sign up to request clarification or add additional context in comments.

Comments

0

What you are looking for is array_merge or array_merge_recursive depending on your actual array structure.

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

Example

$arr1 = [1, 2, 3, 4, 5]; $arr2 = [6, 7, 8, 9, 10]; $arr3 = ['name' => 'Script47']; $res = array_merge($arr1, $arr2, $arr3); var_dump($res); 

Output

array(11) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) ["name"]=> string(8) "Script47" } 

Thereafter, you can loop through the resultant array as a single using foreach($array as $key => $value) { }.

Live Example

Repl

3 Comments

array_merge concatenates the arrays, but I think he wants to process them in parallel, like his first example with just two arrays.
I hate array_merge_recursive it has a tendency to turn non array elements into arrays. Which is correct behavior, so really it's my fault for not using array_replace_recursive, but I still hate it.
See this example Sandbox 'One' and 'Seven' become arrays.
0

Technically you can't loop over more then one "Thing" at a time.

But, if the arrays are correlated, (related). So say array1 => item1 is related to array2 => item1 is related to array3 => item1. Then I would suggest structuring your arrays like this:

Take this example

$array1 = [a1-item1,a1-item2,a1-item3]; $array2 = [a2-item1,a2-item2,a2-item3]; $array3 = [a3-item1,a3-item2,a3-item3]; 

And structure it like this (basically a matrix)

$combined1 = [ [a1-item1,a1-item2,a1-item3], [a2-item1,a2-item2,a2-item3], [a3-item1,a3-item2,a3-item3] ]; 

Then array_column(0) gives you all item1's, and $combined[0] gives you all of array1.

Conversely you can do it the opposite way too.

 $combined2 = [ [a1-item1,a2-item1,a3-item1], [a1-item2,a2-item2,a3-item2], [a1-item3,a2-item3,a3-item3] ]; 

And then array_column(0) gives you array1, and $combined[0] gives you all of item 1's.

Which ever way makes more sense for what you are doing. The only difference is what you get in the foreach loop.

 foreach($combined1 as $array){ print_r($array); } //Outputs: [a1-item1,a1-item2,a1-item3] foreach($combined2 as $array){ print_r($array); } //Outputs: [a1-item1,a2-item1,a3-item1] 

And then you can loop over it to your heart's content whichever way you want.

Then you could flip this middle one like

$combined1 = [ [a1-item1,a1-item2,a1-item3], [a2-item3,a2-item2,a2-item1], [a3-item1,a3-item2,a3-item3] ]; 

Just kidding, don't do that it will mess you all up... lol

Make sense.

Comments

0

Why it's not working

array_combine only accepts two arguments:

http://php.net/manual/en/function.array-combine.php

You can neither use foreach like that. You may only have one key and one value for each foreach, but you can use a foreach inside another foreach.

Something like array_combine with undefinable arguments number

Assuming that you really want to use that functionality, but with several arrays at the same time, you could implement something like this if you're using PHP 7 or a greater version:

<?php function array_combine_many(array ...$keysAndValues) { $length = count($keysAndValues); if ($length % 2 != 0) { throw new \Error("The number of arguments should be even. It's: $length."); } $combinations = array(); for ($i = 0; $i < $length; ++$i) { $combinations[] = array_combine( $keysAndValues[$i], $keysAndValues[++$i] ); } return $combinations; } 

If you're not using PHP 7 or a greater version, you have to use the func_get_args function:

<?php function array_combine_many() { $keysAndValues = func_get_args(); $length = count($keysAndValues); // etc... 

foreach inside a foreach

You may use the implemented function like this:

<?php $val1 = ['a', 'b', 'c']; $val2 = ['12', '34', '56']; $val3 = ['d', 'e', 'f', 'g']; $val4 = ['156', '67', '5678', '346']; $val5 = ['h']; $val6 = ['3487']; $combinations = array_combine_many($val1, $val2, $val3, $val4, $val5, $val6); foreach ($combinations as $combination) { foreach ($combination as $key => $val) { echo "$key => $val\n"; } } 

Output:

a => 12 b => 34 c => 56 d => 156 e => 67 f => 5678 g => 346 h => 3487 

Using array_map, array_merge and call_user_func_array

Here's another solution:

<?php $merged = call_user_func_array( 'array_merge', array_map( function (array $args) { return array_combine($args[0], $args[1]); }, [ [$val1, $val2], [$val3, $val4], [$val5, $val6] ] ) ); foreach ($merged as $key => $value) { echo "$key => $value\n"; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.