array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'en', ), 'this' => array ( 0 => 'class.callbackfilteriterator.php', 1 => 'CallbackFilterIterator', ), 'up' => array ( 0 => 'spl.iterators.php', 1 => 'Iterators', ), 'prev' => array ( 0 => 'cachingiterator.valid.php', 1 => 'CachingIterator::valid', ), 'next' => array ( 0 => 'callbackfilteriterator.accept.php', 1 => 'CallbackFilterIterator::accept', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/spl/callbackfilteriterator.xml', ), 'extra_header_links' => array ( 'rel' => 'alternate', 'href' => '/manual/en/feeds/class.callbackfilteriterator.atom', 'type' => 'application/atom+xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.4.0, PHP 7, PHP 8)
The callback should accept up to three arguments: the current item, the current key and the iterator, respectively.
Example #1 Available callback arguments
<?php
/**
* Callback for CallbackFilterIterator
*
* @param $current Current item's value
* @param $key Current item's key
* @param $iterator Iterator being filtered
* @return boolean TRUE to accept the current item, FALSE otherwise
*/
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();
});
?>