php array_walk() 数组函数
函数array_walk():单一数组回调函数---对数组中的每个成员应用用户函数
代码如下:
/*函数array_walk():单一数组回调函数---对数组中的每个成员应用用户函数
* 1、语法:bool array_walk ( array &array, callback funcname [, mixed $userdata] )
* 2、描述:如果成功则返回 TRUE,失败则返回 FALSE
* 3、注意事项:
* 3.1、$funcname是用户自己 定义的回调函数,接受2个参数,第一个参数为数组$array的值,第二个参数为
* 数组$array的键名,如果提供第三个参数$userdata,将作为第三个参数传递给回调函数$funcname
* 3.2、使用回调函数可以直接更改数组各个单元的值,但更改各个键名是无效的
* 3.3、该函数 不会受到 array 内部数组指针的影响。array_walk() 会遍历整个数组而不管指针
* 的位置
* 3.4、用户不应在回调函数中改变该数组本身,例如增加/删除单元,unset 单元等等,如果 array_walk()
* 作用的数组改变了,则此函数的的行为未经定义,且不可预期。
*/
$words=array("l"=>"lemon","o"=>"orange","b"=>"banana","a"=>"apple");
//定义一个回调函数,输出数组元素
function words_print($value,$key,$prefix){
echo "$prefix:$key=>$value
\n";
}
//定义一个回调函数直接改变元素的值
function words_alter(&$value,$key){
$value=ucfirst($value);
$key=strtoupper(key);
}
//输出元素的值
array_walk($words,'words_print','words');
//改变元素的值
array_walk($words,'words_alter');
echo "
"; <br>print_r($words); <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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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

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

On November 26, 2020, the PHP8 version was officially released, which brought many new features and bug fixes. One of the things that has attracted the attention of many developers is the new application method of the array function array_key_first(). In the past, we might have used the array_shift() function to get the key of the first element of an array. But there is a problem with this method, that is, NULL will be returned when the array is empty. In PHP8, we can use the array_key_first() function
