How can the use of FilterIterator and RecursiveIteratorIterator help in searching a multi-dimensional array in PHP?

When searching a multi-dimensional array in PHP, using FilterIterator and RecursiveIteratorIterator can help iterate through the nested arrays efficiently. FilterIterator can be used to filter out specific elements based on a condition, while RecursiveIteratorIterator can help iterate through the nested arrays in a recursive manner.

<?php
// Sample multi-dimensional array
$array = [
    'fruits' => ['apple', 'banana', 'cherry'],
    'colors' => ['red', 'green', 'blue'],
    'numbers' => [1, 2, 3],
];

// Create a RecursiveIteratorIterator to iterate through the nested arrays
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

// Create a FilterIterator to filter out elements based on a condition
class CustomFilter extends FilterIterator {
    public function accept() {
        $current = $this->current();
        // Add your filter condition here
        return is_string($current);
    }
}

// Use the CustomFilter with the RecursiveIteratorIterator
$filteredIterator = new CustomFilter($iterator);

// Iterate through the filtered elements
foreach ($filteredIterator as $value) {
    echo $value . PHP_EOL;
}
?>