OpenSwoole\Coroutine\map

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\map(array $list, callable $fn, float $timeout = -1): array

Parameters

list

The array list will be operated on with $fn for each element.

fn

The callback function to be executed for each element in the array.

timeout

The total timeout to wait for all callbacks to finish, it will return immediately after timeout. But the running coroutine will continue to execute to completion without stopping.

Return

Description

Must have at least OpenSwoole v4.5.5

Similar to array_map, a callback function is applied to each element of the array.

<?php
OpenSwoole\Coroutine\map(array $list, callable $fn, float $timeout = -1): array

Example

<?php
use function OpenSwoole\Coroutine\map;

function fatorial(int $n): int
{
    return array_product(range($n, 1));
}

co::run(function () {
    $results = map([2, 3, 4], 'fatorial');
    print_r($results);
});
Last updated on September 1, 2022