LINUX.ORG.RU

История изменений

Исправление Obezyan, (текущая версия) :

Не скажу за coq, но в PHP это любая обезъяна сделает, как с лямбдой:

<?php
$arr1 = ['v1', 2, 'three', 4];
$arr2 = ['first', 'second', 3];

$result = array_map(fn($k, $v): string => $k." is the ".$v, $arr1, $arr2);

print_r($result);

Результат:

Array
(
    [0] => v1 is the first
    [1] => 2 is the second
    [2] => three is the 3
    [3] => 4 is the 
)

Так и без нее обычной функцией (добавил проверок на тип значения чтобы вывод был более понятный):

<?php
$arr1 = ['v1', 2, 'three', 4];
$arr2 = ['first', 'second', 3];

function describe($k, $v) {
    if (!$k) {
        $k = gettype($k);
    }
    if (!$v) {
        $v = gettype($v);
    }

    return $k." is the ".$v;
}

$result = array_map('describe', $arr1, $arr2);

print_r($result);

Результат:

Array
(
    [0] => v1 is the first
    [1] => 2 is the second
    [2] => three is the 3
    [3] => 4 is the NULL
)

Исходная версия Obezyan, :

Не скажу за coq, но в PHP это любая обезъяна сделает, как с лямбдой:

<?php
$arr1 = ['v1', 2, 'three', 4];
$arr2 = ['first', 'second', 3];

$result = array_map(fn($k, $v): string => "$k is the ".$v, $arr1, $arr2);

print_r($result);

Результат:

Array
(
    [0] => v1 is the first
    [1] => 2 is the second
    [2] => three is the 3
    [3] => 4 is the 
)

Так и без нее обычной функцией (добавил проверок на тип значения чтобы вывод был более понятный):

<?php
$arr1 = ['v1', 2, 'three', 4];
$arr2 = ['first', 'second', 3];

function describe($k, $v) {
    if (!$k) {
        $k = gettype($k);
    }
    if (!$v) {
        $v = gettype($v);
    }

    return "$k is the ".$v;
}

$result = array_map('describe', $arr1, $arr2);

print_r($result);

Результат:

Array
(
    [0] => v1 is the first
    [1] => 2 is the second
    [2] => three is the 3
    [3] => 4 is the NULL
)