2

I have two arrays in my code. Need to perform a foreach loop on both of these arrays at one time. Is it possible to supply two arguments at the same time such as foreach($array1 as $data1 and $array2 as $data2) or something else?

gen_Eric
228k42 gold badges304 silver badges343 bronze badges
asked Jan 13, 2012 at 19:02
2
  • Why do you need to loop both arrays at the same time? Commented Jan 13, 2012 at 19:08
  • 1
    Will they have the same number of elements? Commented Jan 13, 2012 at 19:09

5 Answers 5

2

If they both the same keys, you can do this:

foreach($array1 as $key=>data1){
 $data2 = $array2[$key];
}
answered Jan 13, 2012 at 19:10
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming both have the same number of elements

If they both are 0-indexed arrays:

foreach ($array_1 as $key => $data_1) {
 $data_2 = $array_2[$key];
}

Otherwise:

$keys = array_combine(array_keys($array_1), array_keys($array_2));
foreach ($keys as $key_1 => $key_2) {
 $data_1 = $array_1[$key_1];
 $data_2 = $array_2[$key_2];
}
answered Jan 13, 2012 at 19:18

1 Comment

nice solution with the keys mapping, +1
1

Dont use a foreach. use a For, and use the incremented index, eg. $i++ to access both arrays at once.

Are both arrays the same size always? Then this will work:

$max =sizeof($array);
for($i = 0; $i < $max; $i++)
array[$i].attribute
array2[$i].attribute

If the arrays are different sizes, you may have to tweak your approach. Possibly use a while.

iterative methods:

http://php.net/manual/en/control-structures.while.php

http://php.net/manual/en/control-structures.for.php

http://php.net/manual/en/control-structures.do.while.php

answered Jan 13, 2012 at 19:10

2 Comments

never use a function inside a for condition, it will execute it each loop, your example should be instead : for ($i = 0, $max = sizeof($array); $i < $max; $i++)
That's what the for loop is for. I would add the {...}, so one can see that you access the arrays from inside the loop.
0

use for or while loop e.g.

$i = 0; 
while($i < count($ar1) && $i < count($ar2) ) // $i less than both length ! 
{ 
 $ar1Item = $ar1[$i];
 $ar2Item = $ar2[$i];
 $i++;
}
answered Jan 13, 2012 at 19:10

1 Comment

if you're using a while loop, define the counts beforehand as variables, no need to do the count each loop (same as the post below), use while ($i<$max1 && $i<$max2) and define $max1 = count($ar1) and same for ar2 before the while loop
0

No to my knowledge with foreach, but can be easily done with for:

<?php
$cond1 = 0<count($array1);
$cond2 = 0<count($array2);
for ($i=0;$cond1 || $cond2;$i++)
{
 if ($cond1)
 {
 // work with $array1
 }
 if ($cond2)
 {
 // work with $array2
 }
 $cond1 = 0<count($array1);
 $cond2 = 0<count($array2);
}
?>
answered Jan 13, 2012 at 19:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.