Home > Backend Development > PHP Tutorial > PHP function introduction—array_map(): applies a callback function to each element of the array

PHP function introduction—array_map(): applies a callback function to each element of the array

王林
Release: 2023-07-24 19:06:02
Original
1324 people have browsed it

PHP function introduction—array_map(): Apply a callback function to each element of the array

As a widely used programming language, PHP provides a large number of built-in functions to facilitate us to perform various operations. One very useful function is array_map(). The array_map() function applies a callback function to each element of one or more arrays and returns a new array. In this article, we will introduce the usage of array_map() function in detail and sample code.

The usage of the array_map() function is as follows:

array_map ( callable $callback , array $array1 [, array $... ] ) : array

Parameter description :

  • $callback: Callback function, used to process each element of the array. It can be a defined function or an anonymous function.
  • $array1: The array to be processed.
  • $...: Optional more arrays.

Return value: Returns a new array consisting of elements processed by the callback function.

The following is a simple example of how to use the array_map() function to convert each element in an array to uppercase:

<?php
function convert_to_uppercase($value) {
    return strtoupper($value);
}

$names = array("john", "james", "jane", "julie");
$names_uppercase = array_map("convert_to_uppercase", $names);

print_r($names_uppercase);
?>
Copy after login

In the above code, we define a name Is the function of convert_to_uppercase(), which converts the incoming value to uppercase letters and returns it. Then, we created an array called $names that contains some names in lowercase letters. Finally, we use the array_map() function to apply the convert_to_uppercase() function to each element in the $names array and store the result in a new array called $names_uppercase. Finally, we use the print_r() function to print the contents of $new_array.

The output results are as follows:

Array
(
    [0] => JOHN
    [1] => JAMES
    [2] => JANE
    [3] => JULIE
)
Copy after login

As you can see, the array_map() function converts each element in the $names array to uppercase and stores the result in the $names_uppercase array.

In addition to using already defined functions as callback functions, we can also use anonymous functions. Here is an example of using an anonymous function to double each element of an array:

<?php
$numbers = array(1, 2, 3, 4, 5);
$doubled_numbers = array_map(function($value) {
    return $value * 2;
}, $numbers);

print_r($doubled_numbers);
?>
Copy after login

In the above code, we define the callback function by passing an anonymous function to the array_map() function. An anonymous function takes a value and returns twice it. Then, we created an array called $numbers that contains some numbers. Finally, we use the array_map() function to apply the anonymous function to each element in the $numbers array and store the results in a new array called $doubled_numbers. Finally, we use the print_r() function to print the contents of $doubled_numbers.

The output results are as follows:

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

As you can see, the array_map() function doubles each element in the $numbers array and stores the result in the $doubled_numbers array.

In actual development, the array_map() function is often used to convert, filter or operate arrays. By passing different callback functions, we can apply different operations to each element of the array to achieve various needs.

To sum up, the array_map() function is a very useful PHP function that can apply a callback function to each element of the array and return a new array. By rationally utilizing the array_map() function, we can simplify the code for array operations and improve development efficiency.

The above is the detailed content of PHP function introduction—array_map(): applies a callback function to each element of the array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template