Home> php教程> php手册> body text

PHP array_map()函数使用方法

WBOY
Release: 2016-06-21 08:52:36
Original
1377 people have browsed it

定义和用法
array_map() 函数返回用户自定义函数作用后的数组。回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致。
语法
array_map(function,array1,array2,array3...)

参数 描述
function 必需。用户自定义函数的名称,或者是 null。
array1 必需。规定数组。
array2 可选。规定数组。
array3 可选。规定数组。

例子:
function cube($n)
{
$cb = $n*$n*$n;
return $cb;
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);

echo '计算原数组各元素的立方,结果如下:';
echo '
';
echo '

';

print_r($b);
?>

输出:
计算原数组各元素的立方,结果如下:
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)



Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!