How can I skip two arrays at once?

I am trying to put two arrays together and the results remain wrong. I will show you my code, the results that I get, and the results that I am looking for.

I think I'm just doing it wrong, but I don’t know how to do it.

My code is:

$data1 = [ 'a: 1', 'a: 2', 'a: 3', 'a: 4', 'a: 5' ]; $data2 = [ 'b: 1', 'b: 2', 'b: 3', 'b: 4', 'b: 5' ]; foreach($data1 as $item1) { foreach($data2 as $item2) { echo $item1 . '<br/>'; echo $item2 . '<br/>'; echo '<br/><br/>'; } } 

Results: (reduced to save space)

 a: 1 b: 1 a: 1 b: 2 a: 1 b: 3 a: 1 b: 4 a: 1 b: 5 

The results I'm looking for are as follows:

 a: 1 b: 1 a: 2 b: 2 a: 3 b: 3 a: 4 b: 4 a: 5 b: 5 
+4
source share
2 answers

Problem

Well, of course, the problem is that your nested foreach loop. Since for each element of your array $data1 you are $data1 through the entire array of $data2 (so that in the end there is an iteration of $data1 * $data2 ).


Decision

To solve this problem, you need to skip both arrays at once.

array_map() method (PHP> = 5.3)

You can do this with array_map() and pass all arrays that you want to skip at the same time.

 array_map(function($v1, $v2){ echo $v1 . "<br>"; echo $v2 . "<br><br>"; }, $data1, $data2 /* , Add more arrays if needed manually */); 

Note: If the number of elements is uneven, you will not generate any errors, it simply prints NULL (means that you will not see anything)

MultipleIterator method (PHP> = 5.3)

Use MultipleIterator and attach as many ArrayIterator as you need.

 $it = new MultipleIterator(); $it->attachIterator(new ArrayIterator($data1)); $it->attachIterator(new ArrayIterator($data2)); //Add more arrays if needed foreach($it as $a) { echo $a[0] . "<br>"; echo $a[1] . "<br><br>"; } 

Note. If the number of elements is uneven, it simply prints all the values ​​in which both arrays still have values

for loop method (PHP> = 4.3)

Use a for loop with a counter variable, which you can use as a key for both arrays.

 $keysOne = array_keys($data1); $keysTwo = array_keys($data2); $min = min(count($data1), count($data2)); for($i = 0; $i < $min; $i++) { echo $data1[$keysOne[$i]] . "<br>"; echo $data2[$keysTwo[$i]] . "<br><br>"; } 

Note. Using array_keys() is that this also works if arrays do not have the same keys or are associative. min() used only for looping through a set of elements, since each array has

array_combine() method (PHP> = 5.0)

Or, if arrays have only unique values, you can array_combine() both arrays, so $data1 can be obtained as a key and $data2 as a value.

 foreach(array_combine($data1, $data2) as $d1 => $d2) { echo $d1 . "<br>"; echo $d2 . "<br><br>"; } 

Note. Arrays must have the same number of elements, otherwise array_combine() will cause an error

call_user_func_array() method (PHP> = 5.6)

If you want to print more than two arrays at the same time or just an unknown number of arrays, you can combine the array_map() method with the call_user_func_array() call.

 $func = function(...$numbers){ foreach($numbers as $v) echo $v . "<br>"; echo "<br>"; }; call_user_func_array("array_map", [$func, $data1, $data2]); 

Note: Since this method uses the array_map() method, it behaves the same with an uneven number of elements. Also, since this method only works for PHP> = 5.6, you can simply remove the arguments and change $numbers with func_get_args() in the foreach loop, and then it also works for PHP> = 5.3

+20
source

use the key from 1 to get the string at 2

 foreach($data1 as $k=> $item1) { echo $item1 . '<br/>'; echo $data2[$k] . '<br/>'; echo '<br/><br/>'; } 
+2
source

All Articles