PHP calls the callback function to a given array unit.

Source: Internet
Author: User
Tags glob
This article mainly introduces how PHP applies callback functions to a given array Unit. It is a very important application. For more information, see

This article mainly introduces how PHP applies callback functions to a given array Unit. It is a very important application. For more information, see

Arrays are an important part of PHP programming. This article introduces the usage of the Array Function array_map () in PHP to implement the function of the callback to the given array unit. The details are as follows:

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

Array_map () returns an array containing all the elements in arr1 that have passed the callback function.

The number of parameters accepted by callback should be the same as the number of arrays passed to the array_map () function.

The example program is as follows:

Function fun ($ n) {return $ n * $ n;} $ a = array (1, 2, 3, 4, 5 ); $ B = array_map ('fun ', $ a);/* Each array Unit performs a cubic operation and returns an array */print_r ($ B );

Output result:

Array ([0] = & gt; 1 [1] = & gt; 8 [2] = & gt; 27 [3] = & gt; 64 [4] = & gt; 125)

In addition, the array_map () function has the following usage:

Array_map ('unlink', glob ('*. txt ');/* glob returns an array composed of "file name .txt", and then deletes each file */array_map ('unlink', glob ('*. * '); array_map ('unlink', glob ('*'));

If array_map () is not used, operations on each cell in the array can only be traversed and assembled as appropriate.

More application readers can explore specific project requirements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.