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

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP 中的 array_map() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 478 ::
收藏到网摘: 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

登陆 | 还没注册?