10

I'm new to PHP. I have two arrays $array1 and $array2 of equal size. I've been using foreach loops to iterate through arrays like so:

foreach($array1 as $element1) {
      //Do stuff with $element1
}

and

foreach($array2 as $element2) {
      //Do stuff with $element2
}

but now I'd like to iterate through both arrays at the same time so that I have access to both $element1 and $element2 in the loop body.

How do I do that?

hakre
  • 193,403
  • 52
  • 435
  • 836
Adam
  • 43,763
  • 16
  • 104
  • 144

5 Answers5

10
while (($element1 = next($array1)) !== false) {
  $element2 = next($array2);
  // Do something
}

But it will fail, if false is an allowed value in $array1. If (in this case) false is not allowed in $array2, you can just swap both

A "foreach"-solution (if both shares the same key)

foreach ($array1 as $i => $element1) {
  $element2 = $array2[$i];
  // Do something
}

A third (I think quite nice) solution, that just allows primitive types in $array1

foreach (array_combine(array_values($array1), array_values($array2)) as $element1 => $element2) {
  // Do something
}
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • The `next` trick will work as long as there are no `false`s in the first array. – Charles Apr 12 '11 at 21:11
  • Yes, I know, but I didnt found a (not so complex) solution for that. However, I added two other ways and at least one should fit :) The third one feels quite nice to me, but it has its limitations, too. – KingCrunch Apr 12 '11 at 21:15
2

Each returns an array containing the key and value, and advances the pointer to the next element. It returns false once it has advanced past the last element.

// Iterate until one of the arrays is complete
while (($a = each($array_a)) !== false && ($b = each($array_b)) !== false) {
    echo "The key:value from array_a is {$a['key']}:{$a['value']}.\n";
    echo "The key:value from array_b is {$b['key']}:{$b['value']}.\n";
}

To iterate completely over both arrays, use || instead of &&.

beefsack
  • 428
  • 1
  • 5
  • 10
1

use a for loop instead...

for($i = 0;$i<count($array1);$i++) { 
    /* access $array1[$i] and $array2[$i] here */ 
}

This will work if the indexes of the arrays are numeric and the same for both arrays

Devin Crossman
  • 7,454
  • 11
  • 64
  • 102
  • Only works for numerical arrays. – Felix Kling Apr 12 '11 at 21:09
  • this is not **foreach** loop... question `Can I iterate through two loops of equal size with foreach?`But yeah, it works for nums as keys :) – Wh1T3h4Ck5 Apr 12 '11 at 21:11
  • 1
    @Wh1T3h4Ck5, there are more ways to iterate over an array in PHP than there are to skin a cat. All of them *can* be appropriate in a given circumstance. – Charles Apr 12 '11 at 21:12
0

That is a possible solution. If you start with next() directly, you never get the first element of the array.

reset($array1); reset($array2);
for ($element1 = current($array1), $element2 = current($array2); 
        $element1 !== false && $element2 !== false; 
        $element1 = next($array1), $element2 = next($array2)) {
    // Do something

}
Aison
  • 61
  • 3
0

What about

$i = 0;
foreach($array1 as $element) {

    // Do stuff with the element
    $stuff_from_this_array = $element;
    $stuff_from_other_array = $array2[$i];
    $i++;

}

You'd obviously want to put $stuff_this_array and $stuff_from_other_array into something more persistent, but maybe this gives you an idea.

Charles
  • 50,943
  • 13
  • 104
  • 142
jbnunn
  • 6,161
  • 4
  • 40
  • 65