Home php教程 php手册 php array_map()数组函数使用说明_php入门_脚本之家

php array_map()数组函数使用说明_php入门_脚本之家

Jun 06, 2016 pm 08:38 PM
array_map array function

函数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 "
Copy after login
";
//定义对单个数组处理的回调函数
function change_value($value){
return ucfirst($value).".com";
}
$urls=array_map('change_value',$websites);
echo "
"; <br>print_r($urls); <br>echo "
Copy after login
";
//多个数组使用的例子
$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 "
Copy after login
";

运行效果如下:

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Array functions in PHP8: efficient use of array_pad() Array functions in PHP8: efficient use of array_pad() May 16, 2023 pm 02:00 PM

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

Array functions in PHP8: multiple uses of array_unique() Array functions in PHP8: multiple uses of array_unique() May 17, 2023 am 08:13 AM

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

Quick methods to change the array structure: array_map, array_reduce, etc. Quick methods to change the array structure: array_map, array_reduce, etc. Jun 20, 2023 pm 04:51 PM

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

Array functions in PHP8: various operating techniques of array_slice() Array functions in PHP8: various operating techniques of array_slice() May 15, 2023 pm 10:43 PM

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

Detailed explanation of how to use array_diff_key() of PHP array function Detailed explanation of how to use array_diff_key() of PHP array function Jun 27, 2023 pm 05:18 PM

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? How to use Go language array function to sum and return the result? Jul 31, 2023 pm 02:25 PM

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

Quick sort using array functions in PHP Quick sort using array functions in PHP Jun 16, 2023 am 08:54 AM

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 PHP8.1 update: performance improvements for array and string functions Jul 08, 2023 am 08:25 AM

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

See all articles