"\n";
}
echo str_repeat("-",30)."\n";
$persons = new ArrayObject( $arr );
$iterator = new GenderFilter( $persons->getIterator() ,"M");
foreach( $iterator as $person )
{
echo $person['name'] . "\n";
}
?>
If you run the code, you will get the following result:
Lily Bernard
Ayesha Siddika
------------------------------
John Abraham
Afif
I'm sure you will agree that this is quite interesting, however did you get the catch?
This is filtered by the following entry point:
public function accept()
{
$person = $this->getInnerIterator()->current();
if( $person['sex'] == $this->GenderFilter )
{
return TRUE;
}
return FALSE;
}
}
Standard PHP Library
[ 158 ]
LimitIterator
What if you want to define the start point from where your iteration will start
and also define the times you want to iterate? This is made possible using
LimitIterator.
LimitIterator takes three parameters while constructing. The first one is a regular
Iterator, the second one is the starting offset, and the third one is the number of times
that it will iterate. Take a look at the following example:
$arr = array(
array("name"=>"John Abraham", "sex"=>"M", "age"=>27),
array("name"=>"Lily Bernard", "sex"=>"F", "age"=>37),
array("name"=>"Ayesha Siddika", "sex"=>"F", "age"=>26),
array("name"=>"Afif", "sex"=>"M", "age"=>2)
);
$persons = new ArrayObject($arr);
$LI = new LimitIterator($persons->getIterator(),1,2);
foreach ($LI as $person) {
echo $person['name'].
Pages:
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173