The CallbackFilterIterator class

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

Introduction

Examples

The callback should accept up to three arguments: the current item, the current key and the iterator, respectively.

Example #1 Available callback arguments

<?php


function my_callback($current, $key, $iterator) {
// Your filtering code here
}

?>

Any callable may be used; such as a string containing a function name, an array for a method, or an anonymous function.

Example #2 Callback basic examples

<?php

$dir
= new FilesystemIterator(__DIR__);

// Filter large files ( > 100MB)
function is_large_file($current) {
return
$current->isFile() && $current->getSize() > 104857600;
}
$large_files = new CallbackFilterIterator($dir, 'is_large_file');

// Filter directories
$files = new CallbackFilterIterator($dir, function ($current, $key, $iterator) {
return
$current->isDir() && ! $iterator->isDot();
});

?>

Table of Contents

To Top