哈希空间 Ctrl + F 进行搜索
首页 php手册中文版 CSS中文手册 哈希文档 Markdown在线工具

PHP callbackfilteriterator 组件

The CallbackFilterIterator class

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

简介

类摘要

class CallbackFilterIterator extends FilterIterator {
/* 方法 */
public __construct(Iterator $iterator, callable $callback)
public accept(): bool
/* 继承的方法 */
public FilterIterator::accept(): bool
public FilterIterator::next(): void
public FilterIterator::rewind(): void
public FilterIterator::valid(): bool
public IteratorIterator::next(): void
public IteratorIterator::rewind(): void
public IteratorIterator::valid(): bool
}

范例

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

示例 #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.

示例 #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();
});

?>

目录

打开 哈希空间 微信小程序中查看更佳