Using array_map() to Apply a Callback to the Elements of an Array in PHP


Modify each element of an array using a callback function.

Source Code

function square($n) {
    return ($n * $n);
}
$a = [1, 2, 3, 4, 5];
$b = array_map('square', $a);
print_r($b);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments