Using PHP's array_map() Function: Syntax, Examples, and Summary
This article explains PHP's array_map() function, detailing its syntax, parameters, and usage through practical examples with both named and anonymous callbacks, demonstrating how to transform arrays efficiently and shows the resulting output for verification.
PHP is a widely used scripting language for web development that provides many built-in functions. The array_map() function is a powerful utility that applies a callback to each element of one or more arrays and returns a new array with the processed values.
array_map() Function Syntax
<code>array_map(callback, array1, array2...)</code>The callback is the function to be executed, and array1 , array2 , etc., are the arrays to be processed. array_map() applies the callback to each element of the provided arrays in order and returns a new array containing the results.
Below is a simple example that squares each number in an array of five integers:
<code>// Define the callback function
function square($num) {
return $num * $num;
}
// Define the array to process
$numbers = [1, 2, 3, 4, 5];
// Apply array_map() with the callback
$result = array_map('square', $numbers);
// Output the result
print_r($result);
</code>The output is:
<code>Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
</code>In this example, we first define a named callback function square() that returns the square of its argument. We then create an array $numbers containing five numbers and call array_map() to apply square() to each element, storing the transformed values in $result . Finally, print_r() displays the new array.
Besides named callbacks, anonymous functions can also be used. The following example converts each string in an array to uppercase using an anonymous function:
<code>// Define the array to process
$strings = ["hello", "world", "php", "array", "map"];
// Use an anonymous function as the callback
$result = array_map(function($string) {
return strtoupper($string);
}, $strings);
// Output the result
print_r($result);
</code>The output is:
<code>Array
(
[0] => HELLO
[1] => WORLD
[2] => PHP
[3] => ARRAY
[4] => MAP
)
</code>Here, an anonymous function receives each string, converts it to uppercase with strtoupper() , and returns the result. array_map() processes each element of $strings , stores the transformed values in $result , and print_r() displays the final array.
Summary
The array_map() function is a very useful array-processing tool in PHP. It can apply any callable—named or anonymous—to each element of one or more arrays, returning a new array with the processed values. The examples above demonstrate basic usage and show how array_map() simplifies array transformations, making development more efficient.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.