Book("50 shades of gray"), 5 new Book("The Hobbit"), 6 new Book("It"), 7 array( 8 new Book("Discworld: The colour of magic"), 9 new Book("Discworld: A light fantastic"), 10 ), 11 array( 12 new Book("Harry Potter and the deadly hallows"), 13 /* ... */ 14 ), 15 /* ... */ 16 ); 17 18 foreach ($bookCollection as $book) { 19 // Do something with a book, but how about book-series? 20 }
array( 5 new Book("50 shades of gray"), 6 new Book("The Hobbit"), 7 new Book("It"), 8 array('Discworld' => 9 new Book("The colour of magic"), 10 new Book("A light fantastic"), 11 ), 12 array('Harry Potter' => 13 new Book("Harry Potter and the deadly hallows"), 14 /* ... */ 15 ), 16 /* ... */ 17 )); 18 19 foreach ($bookCollection as $book) { 20 // Do something with all single books 21 } 22 23 foreach ($bookCollection->getSeries('Harry Potter')) { 24 // Do something with just one serie 25 } 26 27 $it = $bookCollection->getTopRatedBooks(); 28 $it2 = new LimitIterator($it, 0, 5); 29 foreach ($it2 as $book) { 30 // Do something with the top 5 31 } 32 33 $it = $bookCollection->getTopRatedBooks(); 34 $it2 = new RegexIterator($it, '/(gray|colour)/'); 35 foreach ($it2 as $book) { 36 // Do something with all books that have 'gray' or 'colour' in the title 37 }
BC) ➡ Documentation is not always up to date. ➡ Naming is VERY confusing (caching iterator, recursiveIterator, seekableIterator) ➡ But the iterators are worth it! 49
➡ Big-Oh O(1), O(n), O(log n) etc... ➡ Balance between time (CPU) and space (memory) ➡ PHP arrays are quite good! ➡ But sometimes other data structures are better.