Home > Backend Development > PHP Tutorial > php array_map() array function usage instructions_PHP tutorial

php array_map() array function usage instructions_PHP tutorial

WBOY
Release: 2016-07-21 15:27:06
Original
969 people have browsed it

Copy code The code is as follows:

/*Function array_map() function: multi-array callback function---apply the callback function to
* 1. Syntax: array array_map (callback callback, array arr1 [, array ...])
* 2. Description: Returns an array that contains all the cells in arr1
* unit after callback has been applied. The number of arguments accepted by callback should match the number of arrays passed to the array_map() function.
* 3. Notes:
* 3.1. When the multi-array callback function acts on an array, the key name of the original array will be retained, that is, the key name of the returned array is
* when applied to Define the key name of the array
* 3.2. When the multi-array return function operates on two or more arrays, their lengths must be consistent, and the
* key names of the original multiple arrays will be ignored and numbers will be assigned uniformly. Index as key name
*/
//Example of using a single array
$websites=array("g"=>"google","b"=>"baidu","y" =>"yahoo");
//Output the original array
echo "
"; <br>print_r($websites); <br>echo "
";
//Define a callback function for processing a single array
function change_value($value){
return ucfirst($value).".com";
}
$urls=array_map('change_value ',$websites);
echo "
"; <br>print_r($urls); <br>echo "
";
//Example of using multiple arrays
$arr1=array(1,3,5,7);
$arr2=array(2,4,6,8);
//Define a callback function for processing multiple arrays
function func1($a,$b){
return $a*$b;
}
$results=array_map('func1',$arr1,$arr2);
echo "Use callback After the function processes multiple arrays, the result returned:
";
echo "
"; <br>print_r($results); <br>echo "
";

The operation effect is as follows:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323850.htmlTechArticleCopy the code The code is as follows: /*Function array_map() function: multi-array callback function---apply the callback function To the unit of the given array* 1. Syntax: array array_map ( callback callback, ar...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template