What are the advantages of using FilterIterator in PHP for array manipulation?

Using FilterIterator in PHP allows for more efficient and flexible array manipulation by providing a way to iterate over an array and filter out elements based on a specified condition. This can help simplify code and make it easier to work with arrays by removing the need for manual iteration and filtering.

// Create an array of numbers
$numbers = [1, 2, 3, 4, 5];

// Create a FilterIterator to filter out even numbers
class EvenNumberFilter extends FilterIterator {
    public function accept() {
        return $this->current() % 2 == 0;
    }
}

// Create a new iterator using the EvenNumberFilter
$evenNumbers = new EvenNumberFilter(new ArrayIterator($numbers));

// Iterate over the filtered array and output the results
foreach ($evenNumbers as $number) {
    echo $number . "\n";
}