iterator_apply
Call a user-defined function for each element in the iterator
Function name: iterator_apply()
Applicable version: PHP 5, PHP 7
Function Description: the iterator_apply() function applies a function to each element in the iterator.
Syntax: iterator_apply(Iterator $iterator, callable $function [, array $args = NULL])
parameter:
Return value: Returns the result of the function call when successful, and returns FALSE when failure.
Example:
// 创建一个数组迭代器$array = new ArrayIterator(['apple', 'banana', 'cherry']); // 定义一个函数,将每个元素转换为大写function toUpperCase($item) { return strtoupper($item); } // 应用函数到迭代器中的每个元素iterator_apply($array, 'toUpperCase'); // 输出转换后的结果foreach ($array as $item) { echo $item . ' '; // 输出:APPLE BANANA CHERRY }
Notes:
iterator_apply($iterator, 'myFunction', [$arg1, $arg2, $arg3])
.