PHP array function array_map() notes_PHP tutorial

WBOY
Release: 2016-07-13 10:17:15
Original
1084 people have browsed it

PHP array function array_map() notes

Definition and usage

The array_map() function returns the array after the user-defined function is applied. The callback function should accept the same number of arguments as the number of arrays passed to the array_map() function.

Grammar

array_map(function,array1,array2,array3...)

参数 描述
function 必需。用户自定义函数的名称,或者是 null。
array1 必需。规定数组。
array2 可选。规定数组。
array3 可选。规定数组。

Example 1

<?php
function myfunction($v) {
	if ($v === "Dog") {
		return "Fido";
	}
	return $v;
}

$a = array("Horse", "Dog", "Cat");
print_r(array_map("myfunction", $a));
?>
Copy after login

Output:

Array ( [0] => Horse [1] => Fido [2] => Cat )

Example 2

Use multiple parameters:

<?php
function myfunction($v1, $v2) {
	if ($v1 === $v2) {
		return "same";
	}
	return "different";
}

$a1 = array("Horse", "Dog", "Cat");
$a2 = array("Cow", "Dog", "Rat");
print_r(array_map("myfunction", $a1, $a2));
?>
Copy after login

Output:

Array ( [0] => different [1] => same [2] => different )

Example 3

Please see what happens when the custom function name is set to null:

<?php
$a1 = array("Dog", "Cat");
$a2 = array("Puppy", "Kitten");
print_r(array_map(null, $a1, $a2));
?>
Copy after login

Output:

Array (
[0] => Array ( [0] => Dog [1] => Puppy )
[1] => Array ( [0] => Cat [1] => Kitten )
)

Articles you may be interested in

  • PHP array function array_walk() notes
  • PHP generates continuous numeric (letter) array function range() analysis, PHP Lottery program function
  • php pushes elements to the head of the array (usage of array_unshift)
  • Using php functions in smarty templates and how to use multiple functions for one variable in smarty templates
  • How PHP uses the filter function to verify email, url and IP address
  • The difference between PHP merging array + and array_merge
  • Introduction to the union, intersection and difference functions of arrays in PHP
  • php finds whether a certain value exists in the array (in_array(), array_search(), array_key_exists())

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/894201.htmlTechArticlePHP array function array_map() notes definition and usage array_map() function returns the array after the user-defined function is applied. The number of arguments accepted by the callback function should be the same as the number of arguments passed to the array_map() function...
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 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!