<?php
/**
* Iterator that filters every null value out
*/
class NullFilterIterator extends FilterIterator
{
public function accept()
{
return null !== $this->current();
}
}
$iterator = new ArrayIterator([1, null, 3, 4, null]);
echo 'Iterator values:';
foreach ($iterator as $value) {
dump($value);
}
$filter = new NullFilterIterator($iterator);
echo 'FilterIterator values:';
foreach ($filter as $value) {
dump($value);
}
^ 1
^ null
^ 3
^ 4
^ null
^ 1
^ 3
^ 4