From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Merging values from different iterators with MultipleIterator

Merging values from different iterators with MultipleIterator - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Merging values from different iterators with MultipleIterator

- The MultipleIterator class merges values from two or more iterators, but in a fundamentally different way from AppendIterator. AppendIterator loops through values consecutively in the order they were appended. MultipleIterator, on the other hand, processes values in parallel, generating merged Arrays. The difference should become clear with the code in this page, append_multiple.php which you can find in the Chapter Five folder of the Exercise Files. It contains three Arrays of names, boys, girls, and unisex, each of which is passed to ArrayIterator, making it available for chaining with other iterators. All three Arrays are appended to an instance of AppendIterator, and then a foreach loop displays each value. Line 16 creates an instance of MultipleIterator, and the same three Arrays are attached using the attachIterator method. Notice that MultipleIterator uses attachIterator, whereas AppendIterator uses append. Then, on Lines 23 to 25, a foreach loop displays the results of…

Contents