Example of how to use array_map() in PHP

Saul Goodman September 15, 2019 Loading...

Below is a very simple example of how to use array_map() function in PHP.

<?php
$my_arr = [1, 2, 3, 4, 5];
function double($x){
    return $x * 2;
};

$new_arr = array_map('double', $my_arr);

//display the result
echo '<pre>';
print_r($new_arr);
echo '</pre>';

The result:

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)

Related posts

avatar
Welcome to KINDACODE. Have a nice day and code well!