WP_CLI\Utils
iterator_map()
Like array_map(), except it returns a new iterator, instead of a modified array.
Example:
$arr = array('Football', 'Socker');
$it = iterator_map($arr, 'strtolower', function($val) {
return str_replace('foo', 'bar', $val);
});
foreach ( $it as $val ) {
var_dump($val);
}
No Hooks.
Returns
Object. An iterator that applies the given callback(s).
Usage
iterator_map( $it, $fn );
- $it(array|object) (required)
- Either a plain array or another iterator.
- $fn(callable) (required)
- The function to apply to an element.
iterator_map() iterator map code WP-CLI 2.13.0-alpha
function iterator_map( $it, $fn ) {
if ( is_array( $it ) ) {
$it = new ArrayIterator( $it );
}
if ( ! method_exists( $it, 'add_transform' ) ) {
$it = new Transform( $it );
}
foreach ( array_slice( func_get_args(), 1 ) as $fn ) {
$it->add_transform( $fn );
}
return $it;
}