Example of using Array_map array callback function in PHP

Source: Internet
Author: User

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 code is as follows Copy Code

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

$a = Array (1, 2, 3, 4, 5);
$b = Array_map (' fun ', $a); /* Each array unit to do three-time operation, return the array * *
Print_r ($b);
/*
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
*/

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.

Let's look at an example.

  code is as follows copy code

//single Array Use example
$websites =array (" G "=>" Google "," B "=>" Baidu "," Y "=> "Yahoo");
//Output original array
echo "<pre>";
Print_r ($websites);
echo "</pre>";
//define callback functions to be processed on a single array
function Change_value ($value) {
return Ucfirst ($value). ". COM ";
}
$urls =array_map (' Change_value ', $websites);
Echo <pre>;
Print_r ($urls);
Echo </pre > ";
//Examples of multiple arrays
$arr 1=array (1,3,5,7);
$arr 2=array (2,4,6,8)
//define callback functions that handle multiple arrays
function func1 ($a, $b) {
Return $a * $b;
}
$results =array_map (' func1 ', $arr 1, $arr 2);
echo "After processing multiple arrays with a callback function, the result returned:<br>";
Echo <pre > ";
Print_r ($results);
echo "</pre>";

There are friends who do not know the difference between Array_walk and Array_map, the following I briefly introduce

The map is primarily designed to get the new array of your callback functions, which is the result.

The main walk is to use your callback function once for each parameter, which is the process of processing.

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.