函数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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool