Home >Backend Development >PHP Tutorial >PHP function master: array_map()

PHP function master: array_map()

WBOY
WBOYOriginal
2023-06-19 23:42:094829browse

PHP Function Master: array_map()

In the PHP function library, there is a very practical function, that is the array_map() function. It can pass the data in an array to a function for processing, and finally return a new array. The array_map() function can greatly facilitate our data processing. Let's introduce its use in detail below.

1. Basic usage of array_map() function

The basic syntax format of array_map() is:

array_map(callable $callback, array ...$arr)

Among them, the $callback parameter represents the function or method to be called, which corresponds to each element in the array. The $arr parameter represents the array we want to process, and multiple arrays can be passed in for processing at the same time.

Let’s look at a simple example:

$arr = [1, 2, 3];
$newArr = array_map(function($v) {
    return $v * 2;
}, $arr);
print_r($newArr);

After running this code, we will get a new array: Array ( [0] => 2 [1] => ; 4 [2] => 6 ). This new array is the result of multiplying each element in the original array by 2.

2. Advanced usage of array_map() function

In addition to basic usage, array_map() function has many advanced usages. For example, we can process multiple arrays or pass multiple parameters in the callback function.

When processing multiple arrays, we only need to add multiple arrays after the function, for example:

$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
$newArr = array_map(function($v1, $v2) {
    return $v1 + $v2;
}, $arr1, $arr2);
print_r($newArr);

In the above example, we add two arrays The elements in are added, and the final new array is: Array ( [0] => 5 [1] => 7 [2] => 9 ).

In addition, we can also pass multiple parameters in the callback function. We only need to add the parameters that need to be passed after the array parameters. For example:

$arr = [1, 2, 3];
$prefix = 'num:';
$newArr = array_map(function($v, $prefix) {
    return $prefix.$v;
}, $arr, array_fill(0, count($arr), $prefix));
print_r($newArr);

In the above example, we added a $prefix variable and added the "num:" prefix in front of each array element. The final new array is: Array ([0] = > num:1 [1] => num:2 [2] => num:3 ).

Summary

The array_map() function is a very convenient PHP function, which can provide great convenience for us to process arrays. By passing different parameters, we can implement different operations on the array. When using it, we need to pay attention to the use of the callback function and the number of parameters passed, so that we can get the results we need.

The above is the detailed content of PHP function master: array_map(). 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