在线文档教程
PHP
SPL/Iterators

CallbackFilterIterator (class)

The CallbackFilterIterator class

Introduction

(PHP 5 >= 5.4.0, PHP 7)

Class synopsis

CallbackFilterIterator extends FilterIterator implements OuterIterator {

/* Methods */

public __construct ( Iterator $iterator , callable $callback )

public string accept ( void )

/* Inherited methods */

public abstract bool FilterIterator::accept ( void )

public FilterIterator::__construct ( Iterator $iterator )

public mixed FilterIterator::current ( void )

public Iterator FilterIterator::getInnerIterator ( void )

public mixed FilterIterator::key ( void )

public void FilterIterator::next ( void )

public void FilterIterator::rewind ( void )

public bool FilterIterator::valid ( void )

}

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 /**  * 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( } ?>

Table of Contents

  • CallbackFilterIterator::accept — Calls the callback with the current value, the current key and the inner iterator as arguments

  • CallbackFilterIterator::__construct — Create a filtered iterator from another iterator

← CachingIterator::valid

CallbackFilterIterator::accept →

© 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

https://secure.php.net/manual/en/class.callbackfilteriterator.php