PHP's method of acting a callback function on a given array element _php tips

Source: Internet
Author: User
Tags glob php programming

Arrays are a very important part of PHP programming. This article describes the use of the array function Array_map () in PHP to effect the callback function on a given array cell. Specifically as follows:

Array Array_map (callable $callback, array $arr 1 [, Array $ ...])

Array_map () returns an array that contains the cells of all cells in the arr1 after the callback function.

The number of arguments accepted by callback should be consistent with the number of arrays passed to the Array_map () function.

The sample program is as follows:

function Fun ($n)
{return
 $n * $n * $n;
}

$a = Array (1, 2, 3, 4, 5);
$b = Array_map (' fun ', $a); /* Each array unit is three Times Square operation, returns the array
/Print_r ($b);

The output results are:

Array
(
 [0] => 1
 [1] => 8
 [2] =>
 [3] =>
 [4] =>
)

In addition, the Array_map () function has the following several uses:

Array_map (' unlink ', glob (' *.txt '));/* Glob returns an array of "filename. txt" and deletes each file

/array_map (' unlink ', glob (' *.* '));

Array_map (' unlink ', glob (' * '));

If you do not use Array_map (), each unit of an array can be manipulated and then properly assembled.

More application readers can dig according to the specific project requirements.

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.