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

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 array_map() 函数


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

Definition and Usage
定义和用法

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.
array_map()函数将一个数组中的每个值都发送到用户自定义函数中,并返回这个自定义函数中的数组值。

Syntax
语法

array_map(function,array1,array2,array3...)

Parameter
参数
Description
描述
function Required. The name of the user-made function, or null
必要函数。指定自定义函数的名称,或者不设定(空值)
array1 Required. Specifies an array
必要函数。指定一个数组
array2 Optional. Specifies an array
可选函数。指定一个数组
array3 Optional. Specifies an array
可选函数。指定一个数组


Tips and Notes
提示

Tip: You can assign one array to the function, or as many as you like.
提示:你可以给函数制定一个数组,数组的数量不限。


Example 1
案例1

<?php
function myfunction($v)
{
if ($v==="Dog")	{	return "Fido";	}
return $v;
}
$a=array("Horse","Dog","Cat");
print_r(array_map("myfunction",$a));
?>

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

Array ( [0] => Horse [1] => Fido [2] => Cat )


Example 2
案例2

Using the more than one parameter.
使用多个参数:

<?php
function myfunction($v1,$v2)
{
if ($v1===$v2)	{	return "same";	}
return "different";
}
$a1=array("Horse","Dog","Cat");
$a2=array("Cow","Dog","Rat");
print_r(array_map("myfunction",$a1,$a2));
?>

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

Array ( [0] => different [1] => same [2] => different )


Example 3
案例3

Look what happens if you assign null as the functionname:
如果你给函数名称指定空值,看看会发生什么:

<?php
$a1=array("Dog","Cat");
$a2=array("Puppy","Kitten");
print_r(array_map(null,$a1,$a2));
?>

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

Array (
[0] => Array ( [0] => Dog [1] => Puppy )
[1] => Array ( [0] => Cat [1] => Kitten )
)

评论 (0) All

登陆 | 还没注册?