This abstract iterator filters out unwanted values for a RecursiveIterator. This class should be extended to implement custom filters. The RecursiveFilterIterator::accept() must be implemented in the subclass.
I had to create a filter to get values greater than a certain number in a multidimensional array.
Hope someone else finds it helpful:
<?php
class GreaterThanFilter extends RecursiveFilterIterator {
protected $limit;
public function __construct(RecursiveIterator $recursiveIter, $limit) {
$this->limit = $limit;
parent::__construct($recursiveIter);
}
public function accept() {
return $this->hasChildren() || strlen($this->current()) >= $this->limit;
}
public function getChildren() {
return new self($this->getInnerIterator()->getChildren(), $this->limit);
}
}
$iter = new RecursiveArrayIterator($array);
$filter = new GreaterThanFilter($iter, 5);
foreach(new RecursiveIteratorIterator($filter) as $key => $value)
{
echo $value . "\n";
}
?>