当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > array_walk() 函数

PHP
php 无限分类的实现
常用PHP代码
windows下安装配置php视频教程
MySQL数据库结构和数据的导出和导入
PHP实现 IP Whois 查询
PHP5 this,self和parent关键字详解
PHP 安全技巧连载 #1[译]
PHP 安全技巧连载 #2[译]
PHP 安全技巧连载 #3[译]
PHP 安全技巧连载 #4[译]
PHP 安全技巧连载 #5[译]
PHP 安全技巧连载 #6[译]
PHP 安全技巧连载 #7[译]
PHP 安全技巧连载 #8[译]
PHP 安全技巧连载 #9[译]
PHP 安全技巧连载 #10[译]
PHP 安全技巧连载 #11[译]
PHP error_reporting的使用
PHP 安全技巧连载 #12
使用PHP做Linux/Unix守护进程

PHP 中的 array_walk() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 455 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The array_walk() function runs each array element in a user-made function. The array's keys and values are parameters in the function. Returns True or False
array_walk()函数将一个数组中的元素应用于用户自定义函数中;该数组中的键和值作为用户自定义函数中的参数。

Syntax
语法

array_walk(array,function,parameter...)

Parameter参数 Description描述
array Required. Specifying an array
必要参数。指定一个数组
function Required. The name of the user-made function
必要参数。指定用户自定义函数的名称
parameter Optional. Specifies a parameter to the user-made function
可选参数。为用户自定义函数指定一个参数


Tips and Notes
提示和注意点

Tip: You can assign one parameter to the function, or as many as you like.
提示:你可以向函数中指定一个或多个参数。

Note: You can change an array element's value in the user-made function by specifying the first parameter as a reference: &$value. (See example 3)
注意:你可以在用户自定义函数的第一个参数中指定“&$value”来改变原数组元素的值。


Example 1
案例1

<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br />";
}
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
array_walk($a,"myfunction");
?>

The output of the code above will be:
上述代码将输出下面的结果:

The key a has the value Cat
The key b has the value Dog
The key c has the value Horse


Example 2
案例2

With a parameter:
附带参数的情况如下:

<?php
function myfunction($value,$key,$p)
{
echo "$key $p $value<br />";
}
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
array_walk($a,"myfunction","has the value");
?>

The output of the code above will be:
上述代码将输出下面的结果:

a has the value Cat
b has the value Dog
c has the value Horse


Example 3
案例3

Change an array element's value. (Notice the &$value)
改变一个数组中元素的值(特别注意“&$value”):

<?php
function myfunction(&$value,$key)
{
$value="Bird;
}
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
array_walk($a,"myfunction");
print_r($a);
?>

The output of the code above will be:
上述代码将输出下面的结果:

Array ( [a] => Bird [b] => Bird [c] => Bird )

评论 (0) All

登陆 | 还没注册?