PHP: Best way to iterate two parallel arrays?

前端 未结 3 1901
眼角桃花
眼角桃花 2020-12-05 12:24

As seen in this other answer, there are several ways to iterate two same-sized arrays simultaneously; however, all of the methods have a rather significant pitfall. Here ar

3条回答
  •  无人及你
    2020-12-05 13:01

    You can use a MultipleIterator:

    $iterator = new MultipleIterator;
    $iterator->attachIterator(new ArrayIterator($array1));
    $iterator->attachIterator(new ArrayIterator($array2));
    
    foreach ($iterator as $values) {
        var_dump($values[0], $values[1]);
    }
    

    You can find more examples concerning the various options in the docs.

提交回复
热议问题