Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^156) CHAPTER 10 ■ SPL ITERATORS
ParentIterator
The ParentIterator is an extended FilterIterator that can filter out nonparent elements from
a RecursiveIterator, to find only keys that have children. Listing 10-14 demonstrates the use
of a ParentIterator.
Listing 10-14. Using a ParentIterator Iterator
$arr = array(
0 => 'a';
1 => array('a','b','c'),
2 => 'b',
3 => array('a','b','c'),
4 => 'c'
);
$arrayIterator = new RecursiveArrayIterator($arr);
$it = new ParentIterator($arrayIterator);
print_r(iterator_to_array($it, false));
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
)
)
DualIterator
The DualIterator offers simultaneous iteration over two iterators. This iterator has a fairly
complex constructor that takes two iterators and flags. The flags control whether the current
and key properties return the left-hand or right-hand side iterator values, or if they should
return both values as an array.
McArthur_819-9C10.fm Page 156 Friday, February 22, 2008 9:08 AM

Free download pdf