Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of array_map() function in PHP function library

Detailed explanation of the usage of array_map() function in PHP function library

WBOY
WBOYOriginal
2023-06-27 10:03:104665browse

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.

1. Introduction to array_map() function

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.

2. Example demonstration of array_map() function

Next, we will demonstrate the use of array_map() function through some examples.

  1. Double all elements in an array
<?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.

  1. Combine the elements of two arrays into one array
<?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 .

  1. Remove empty elements in the array
<?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.

3. Notes on the array_map() function

To use the array_map() function correctly, you should keep the following points in mind:

  1. The parameters passed to the callback function The value is the CURRENT (current) element in each array, and the value changes during the calling loop. If a non-array is passed to the array_map() function, the value will be passed to the callback function individually.
  2. Users can customize the callback function. You can use PHP built-in functions or custom functions.
  3. If you need to pass an anonymous function to the array_map() function, you need to use PHP 5.3 or higher.
  4. array_map() function is sensitive to the starting index value of array. For example, if two arrays are passed with different key names, the smaller key name will be used to construct the returned array.
  5. If the keys and values ​​in the parameter array will be used in the callback function, do not use the array_map() function, but should use a foreach loop to operate.

4. Summary

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!

Statement:
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