Swoole\Coroutine\map

Declaration

<?php
Swoole\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 Swoole v4.5.5

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

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

Example

<?php
use function Swoole\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);
});