Home > Article > Backend Development > Detailed explanation of the usage of array_map() function in PHP function library
In PHP development, arrays are one of the most commonly used data types. The operation functions for arrays are also quite rich. Among them, the array_map() function is a very practical array operation function. It can apply a function to each element of the array and return a new array.
This article will mainly introduce the usage and precautions of the array_map() function, and demonstrate it with examples.
array_map() function is to apply the first parameter (a PHP function) to each element of the incoming array. The result of the elements processed by the function is returned as the elements of the new array. For example, you can use the array_map() function for multiplication of array values, modification of array values to a certain extent, or conversion of the results obtained, etc.
The basic syntax of the array_map() function is as follows:
array array_map ( callable $callback , array $array1 [, array $...] )
Among them, the $callback
parameter indicates that a callback method is to be made for each element, $array1
The parameter represents the array to be processed. Multiple arrays can be used, so that the callback method can pass in multiple parameters, corresponding to the number of parameters passed into the function. You can also set the callback method to null
, so that the array_map() function will directly return each element of the passed array into a new array.
Next, we will demonstrate the use of array_map() function through some examples.
<?php function double($value){ return $value*2; } $a = array(1, 2, 3, 4, 5); $b = array_map("double", $a); print_r($b); ?>
Output result:
Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 )
In the above code, we define the callback function double (), the function of this function is to double the incoming value. Next, we create an array $a
, which contains the numbers 1 to 5. Then, we get a new array $b
by calling the array_map() function and passing in the array $a
and the callback function "double", where each element is an element in the original array twice.
<?php function combine($a, $b){ return $a . $b; } $a = array("a", "b", "c", "d"); $b = array("1", "2", "3", "4"); $combine = array_map("combine", $a, $b); print_r($combine); ?>
Output result:
Array ( [0] => a1 [1] => b2 [2] => c3 [3] => d4 )
In this example, we define the callback function combine (), the function of this function is to combine the two incoming values into a new string. Next, we created two arrays $a
and $b
, which store different values with the same key name. We then create a new array by calling the array_map() function and passing in the two arrays $a
and $b
and the callback function "combine" $combine
.
<?php function removeEmpty($value){ return trim($value); } $array= array("hello", " ", "world", "", "!"); $no_empty = array_filter(array_map("removeEmpty", $array)); var_dump($no_empty); ?>
Output result:
array(3) { [0]=> string(5) "hello" [2]=> string(5) "world" [4]=> string(1) "!" }
In the above code, we defined the callback function removeEmpty(), The function of this function is to remove spaces from the string. Next, we create an array $array
that contains some empty elements. Using the array_map("removeEmpty", $array)
function, we can make a callback function removeEmpty()
and pass each element in the array into the callback function for processing. Finally, use the array_filter()
function to filter out empty elements. The $no_empty
array is obtained, which contains all elements in the original array after removing empty elements.
To use the array_map() function correctly, you should keep the following points in mind:
In this article, we introduce the definition, usage and precautions of the array_map() function in detail. The array_map() function is a very practical array processing function. It can apply a function to each element in the array, and return the result of each element processed by the function into a new array. Through reasonable application, the array_map() function can greatly simplify the process of PHP array operations, allowing programmers to process arrays more conveniently and efficiently.
The above is the detailed content of Detailed explanation of the usage of array_map() function in PHP function library. For more information, please follow other related articles on the PHP Chinese website!