PHP Version
4.0.6+
// Define an array of numbers
$numbers = [1, 2, 3, 4, 5];

// Callback function to double each element
function double($number) {
    return $number * 2;
}

// Use array_map() to apply the callback to each element and create a new array
$doubled_numbers = array_map('double', $numbers);

print_r($doubled_numbers);
// Output: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 )
array_map(?callable $callback, array $array, array ...$arrays): array
// Define an array of numbers
$numbers = [1, 2, 3, 4, 5];

// Callback function to double each element
function double($number) {
    return $number * 2;
}

// Apply the callback function to only one array using array_map()
$doubledNumbers = array_map('double', $numbers);

print_r($doubledNumbers);
/* Output:
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)
*/
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

// Callback function to add corresponding elements of two arrays
function add_arrays($a, $b) {
    return $a + $b;
}

$result = array_map('add_arrays', $array1, $array2);

print_r($result);
/* Output:
Array
(
    [0] => 5
    [1] => 7
    [2] => 9
)
*/
$array1 = [1, 2, 3];
$array2 = [4, 5];

// Callback function to add corresponding elements of two arrays
function add_arrays($a, $b) {
    return $a + $b;
}

$result = array_map('add_arrays', $array1, $array2);

print_r($result);
/* Output:
Array
(
    [0] => 5
    [1] => 7
    [2] => 3
)
*/
$array = [1, 2, 3];

// Callback function expects a reference, but array elements are not passed by reference
function addOne(&$value) {
    $value += 1;
}

array_map('addOne', $array);
<pre><code class="language-php">
$words = ['Hello', 'World', 'PHP', 'ARRAY'];

// Callback function to convert each string element to lowercase
function to_lowercase($word) {
    return strtolower($word);
}

// Use array_map() to apply the callback to each string element and create a new array
$lowercase_words = array_map('to_lowercase', $words);

print_r($lowercase_words);

// Output: Array ( [0] => hello [1] => world [2] => php [3] => array )
$names = ['John', 'Jane', 'Bob'];
$scores = [85, 92, 78];

// Create an associative array by combining names and scores
$students = array_map(function($name, $score) {
    return ['name' => $name, 'score' => $score];
}, $names, $scores);

print_r($students);

/* Output:
Array
(
    [0] => Array ( [name] => John [score] => 85 )
    [1] => Array ( [name] => Jane [score] => 92 )
    [2] => Array ( [name] => Bob [score] => 78 )
)
*/
class User {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
    public function getGreeting() {
        return "Hello, my name is " . $this->name;
    }
}

$users = [new User('Alice'), new User('Bob'), new User('Charlie')];

// Extract greetings from each user object
$greetings = array_map(function($user) {
    return $user->getGreeting();
}, $users);

print_r($greetings);

/* Output:
Array
(
    [0] => Hello, my name is Alice
    [1] => Hello, my name is Bob
    [2] => Hello, my name is Charlie
)
*/