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

[PHP]将回调函数作用到给定数组的单元上

WBOY
Release: 2016-06-06 19:49:42
Original
1082 people have browsed it

-------------------------------------------------------------------------------------------------------- array array_map( callable $callback , array $arr1 [, array $... ] ) array_map()返回一个数组,该数组包含了 arr1 中的所有单元经过 callba

--------------------------------------------------------------------------------------------------------

array array_map ( callable $callback , array $arr1 [, array $... ] )

array_map() 返回一个数组,该数组包含了 arr1 中的所有单元经过 callback 作用过之后的单元。

       callback 接受的参数数目应该和传递给 array_map() 函数的数组数目一致。

<span>function</span> fun(<span>$n</span><span>)
{
    </span><span>return</span> <span>$n</span> * <span>$n</span> * <span>$n</span><span>;
}

</span><span>$a</span> = <span>array</span>(1, 2, 3, 4, 5<span>);
</span><span>$b</span> = <span>array_map</span>('fun', <span>$a</span><span>); /* 每个数组单元作三次方运算,返回数组 */
</span><span>print_r</span>(<span>$b</span>);<br>/*<br>Array
Copy after login
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)<br>*/
Copy after login

<span>array_map</span>('unlink', <span>glob</span>('*.txt'));<em>/* glob返回"文件名.txt"组成的数组,然后对每个文件进行删除操作*/<br><br></em>
Copy after login
array_map('unlink', glob('*.*'));<br><br>array_map('unlink', glob('*'));
Copy after login

如果不使用array_map(),对数组每个单元进行操作就只能遍历然后适当组装。

 

Link: http://www.cnblogs.com/farwish/p/3836837.html

@黑眼诗人 

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
Popular Tutorials
More>
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!