array_map(callable, array1,array2,array3,...) is a very powerful function available in PHP. Basically is a way to transform one or more arrays applying the logic in the callable function and output the result as another array. These are the most common ways to implement this function:

Option 1. Use a callable outside array_map function. Declare a function and call it inside the array_map function:

function processLetters($item)
{
    switch ($item) {
        case 'a':
            $position = 'first';
            break;
        case 'b':
            $position = 'second';
            break;
        case 'c':
            $position = 'third';
            break;
        case 'd':
            $position = 'fourth';
            break;
        case 'e':
            $position = 'fifth';
            break;
        default:
            $position = 'unknown position';
    }
    
    return 'This is ' . $position . ' in the ABC';
}

$array = ['a', 'b', 'c', 'e', 'f'];

$result = array_map('processLetters', $array);

Option 2. Use anonymous function inside the array_map function:

$array = ['a', 'b', 'c', 'e', 'f'];

$result = array_map(
    function ($item): string {
        switch ($item) {
            case 'a':
                $position = 'first';
                break;
            case 'b':
                $position = 'second';
                break;
            case 'c':
                $position = 'third';
                break;
            case 'd':
                $position = 'fourth';
                break;
            case 'e':
                $position = 'fifth';
                break;
            default:
                $position = 'unknown position';
        }
    
        return 'This is ' . $position . ' in the ABC';
}, $array);

In both cases the output is this:

Array
(
    [0] => This is first in the ABC
    [1] => This is second in the ABC
    [2] => This is third in the ABC
    [3] => This is fifth in the ABC
    [4] => This is unknown position in the ABC
)

Option 3. Use a Lambda Function inside array_map:

$array = ['a', 'b', 'c', 'e', 'f'];

$result = array_map(fn($letterInList): string => $letterInList . ' -- from ABC', $array);

The output is:

Array
(
    [0] => a -- from ABC
    [1] => b -- from ABC
    [2] => c -- from ABC
    [3] => e -- from ABC
    [4] => f -- from ABC
)

Option 4. Use the elements of 2 or more arrays in the callable function:

/**
 * @param string $letter
 * @param string $position
 * @return string
 */
function processLetters(string $letter, string $position): string
{
    
    return 'This is letter: ' . $letter . ' is the ' . $position . ' in the ABC';
}

$letters = ['a', 'b', 'c', 'e', 'f'];
$positions = ['first', 'second', 'third', 'fourth', 'fifth'];

$result = array_map('processLetters', $letters, $positions);

Option 5. Use an associative array (also known as dictionary or map) to process keys and values in the callable function:

/**
 * @param string $position
 * @param string $letter
 * @return string
 */
function processLetters(string $position, string $letter): string
{
    
    return 'This is letter: ' . $letter . ' is the ' . $position . ' in the ABC';
}

$array = [
    'first' => 'a',
    'second' => 'b',
    'third' => 'c',
    'fourth' => 'd',
    'fifth' => 'e'
];

$result = array_map('processLetters', array_keys($array), array_values($array));

In both cases the output is:

Array
(
    [0] => This is letter: a is the first in the ABC
    [1] => This is letter: b is the second in the ABC
    [2] => This is letter: c is the third in the ABC
    [3] => This is letter: d is the fourth in the ABC
    [4] => This is letter: e is the fifth in the ABC
)

 

 

refer to array map function in PHP Documentation for more on this.