php array_map()数组函数使用说明_php入门_脚本之家
函数array_map()函数:多数组回调函数---将回调函数作用到给定数组的单元上
代码如下:
/*函数array_map()函数:多数组回调函数---将回调函数作用到给定数组的单元上
* 1、语法:array array_map ( callback callback, array arr1 [, array ...] )
* 2、描述:返回一个数组,该数组包含了 arr1 中的所有单元经过 callback 作用过之后的
* 单元。callback 接受的参数数目应该和传递给 array_map() 函数的数组数目一致。
* 3、注意事项:
* 3.1、多数组回调函数作用于一个数组时,将保留原有数组的键名,也就是返回的数组的键名就是
* 作用到给定数组的键名
* 3.2、多数组回到函数作用于两个或多个数组时,他们的长度要一致,并且将忽略原来多个数组的
* 键名,统一分配数字索引作为键名
*/
//单个数组使用的例子
$websites=array("g"=>"google","b"=>"baidu","y"=>"yahoo");
//输出原数组
echo "
"; <br>print_r($websites); <br>echo "
//定义对单个数组处理的回调函数
function change_value($value){
return ucfirst($value).".com";
}
$urls=array_map('change_value',$websites);
echo "
"; <br>print_r($urls); <br>echo "
//多个数组使用的例子
$arr1=array(1,3,5,7);
$arr2=array(2,4,6,8);
//定义对多个数组处理的回调函数
function func1($a,$b){
return $a*$b;
}
$results=array_map('func1',$arr1,$arr2);
echo "利用回调函数对多个数组处理后,返回的结果:
";
echo "
"; <br>print_r($results); <br>echo "
运行效果如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP8 is the latest PHP version which provides many new functions and improved features, one of which is the array function array_pad(). In this article, we will explore the efficient use of array_pad() function. What is the array_pad() function? The array_pad() function can fill an array to a specified length and return the filled array. This function accepts three parameters: array_pad(array$array,int$leng

Arrays are a very common data type in the PHP programming language. The unique thing about arrays is that they allow us to store multiple related variables at once, and these variables can be manipulated and processed efficiently. In PHP8, there are many useful array functions that can help us optimize our code, one of which is array_unique(). The function of array_unique() is to remove duplicate array elements and return a new array. This function can be used in many situations. Below we will

Arrays are frequently used data structures in programming, and when dealing with arrays, changing their structures is a common requirement. In the PHP language, there are many built-in functions that can be used to accomplish this purpose, such as array_map, array_reduce, etc. This article will introduce and practical applications of these functions. The array_maparray_map function is a variable function in PHP (variable function means that you can use variables as function names in the code). This function is used to convert all elements in an array

In PHP8, array is a very common data structure that is often used to store and process data. Among them, the array_slice() function is a powerful tool that can slice, intercept and split arrays. This article will introduce various operating techniques of this function to help developers make better use of it. 1. Slicing operation The most basic operation of the array_slice() function is slicing. It can obtain a part of the array by specifying the starting position and length. The sample code is as follows: $arr=a

As a popular programming language, PHP's array functions are also very powerful. When you need to compare the key names of two arrays, you can use the array_diff_key() function. This function can help us find out the key names that are in the first array but do not exist in the second array, and compare the differences between the arrays. This article will introduce in detail how to use the array_diff_key() function. Basic usage of array_diff_key() function array_diff

How to use Go language array function to sum and return the result? The Go language provides a wealth of array operation functions, including functions for finding the sum of array elements. Use these functions to conveniently perform sum operations on arrays and return the results. This article will introduce how to use the array function of Go language to sum and return the result, with code examples. First, let’s take a look at arrays in Go language. An array is a data structure that stores a fixed-size sequence of elements. In Go language, the length of the array is fixed, and the type and element of the array

PHP is a very popular programming language and it is widely used for web development. In PHP, array is a very common data type and a very powerful data structure. Because of this, PHP provides many array functions to help developers handle and manipulate arrays. This includes the quick sort function, which helps us sort arrays quickly. Quick sort is a common sorting algorithm. Its basic idea is to divide an array into two sub-arrays, one smaller than the other, through comparison and exchange, and then recursively

PHP8.1 update: Performance improvements for array and string functions The PHP programming language has continued to evolve and improve over time. The recently released version of PHP 8.1 brings many new features and performance enhancements, especially when it comes to array and string functions. These improvements allow developers to handle array and string operations more efficiently, improving overall performance and efficiency. Performance improvements of array functions In PHP8.1, array functions have been improved and optimized. Here are some important examples of performance improvements for array functions: (1
