5

Possible Duplicate:
foreach with three variables add

If I have 3 arrays of the same size, is it possible to use the costruct foreach() to cycle in the same time the 3 arrays?

ex.

$name contains names

$surname contains surnames

$address contains addresses.

Can foreach take elements [1], [2], [.....] in the same time, to print

$name[1], $surname[1], $address[1];

$name[2], $surname[2], $address[2];

and so on?

Community
  • 1
  • 1
Mewster
  • 1,057
  • 3
  • 11
  • 21
  • 2
    Please choose variable names wisely for fairy tale code (or example code as you will), `$1` is not a valid variable name :) – Ja͢ck Dec 28 '12 at 14:36

5 Answers5

21

SPL's multipleIterator is designed for precisely this purpose

$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($array1));
$mi->attachIterator(new ArrayIterator($array2));
$mi->attachIterator(new ArrayIterator($array3));

foreach ( $mi as $value ) {
    list($name, $surname, $address) = $value;
    echo $name , ' => ' , $surname , ' => ' , $address , PHP_EOL;
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Nice. Would this automatically handle the case that the arrays didn't contain the same number of elements? – Sean Bright Dec 28 '12 at 14:41
  • I'm interested in the same question. By the way, thanks, this seems what I was searching for. – Mewster Dec 28 '12 at 14:42
  • Probably not, because of the list construct. If one of the arrays had two indexes I believe it would fail. – David Harris Dec 28 '12 at 14:46
  • You can change the `MultipleIterator` constructor and pass in `MultipleIterator::MIT_NEED_ANY` which will cause the loop to execute for each element in the longest of the arrays. The default is `MultipleIterator::MIT_NEED_ALL` which is will only run while *every* array has an element in the current slot. – Sean Bright Dec 28 '12 at 14:50
  • Take a look at the optional flags for the array constructor - http://www.php.net/manual/en/multipleiterator.construct.php = which shows how to match up the arrays by numeric or associative indexes – Mark Baker Dec 28 '12 at 14:53
  • it perfectly worked for me Mark Baker, thank you so much, it solved my big problem – G'ulomjon Sulaymonov Mar 28 '19 at 06:56
  • Awesome, it still works in PHP 7 and 8. So much thanks – lewishole Jun 18 '21 at 04:28
7

Assuming they are all the same length:

for ($i = 0; $i < count($names); $i++) {
    echo "{$names[$i]}, {$surnames[$i]}, {$addresses[$i]}";
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
4

You can do it like that (if arrays have the same keys):

foreach ($name as $key => $value) {
    //use $name[$key], $surname[$key], $address[$key]
}

$key contains key in $name array

$value = $name[$key]

LukLed
  • 31,452
  • 17
  • 82
  • 107
1

Try this

foreach($arr1 as $i => $val) {
    var_dump($val, $arr2[$i], $arr3[$i]);
}
Stephen
  • 18,597
  • 4
  • 32
  • 33
0

If the array keys are the same you can use key() function.

But you can pass the key to foreach($array as $key => value()){} This way you can refer to the key by the variable without using a function.

Sinan
  • 5,819
  • 11
  • 39
  • 66