The PHP Array function array

Source: Internet
Author: User
Tags array functions return

Definitions and usage

The Array_map () function returns an array of the actions of the user's custom function. The number of arguments accepted by the callback function should be the same as the number of arrays passed to the Array_map () function.

Grammar

Array_map (Function,array1,array2,array3 ...)

Parameters Description
function Necessary. The name of the user's custom function, or null.
Array1 Necessary. The specified array.
Array2 Optional. The specified array.
Array3 Optional. The specified array.

Example 1

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

$a = Array ("Horse", "Dog", "Cat");
Print_r (Array_map ("MyFunction", $a));
? >

Output:

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

Example 2

Use multiple parameters:

<?php
function MyFunction ($v 1, $v 2) {
	if ($v 1 = = $v 2) {return
		"Same";
	}
	return "different";
}

$a 1 = array ("Horse", "Dog", "Cat");
$a 2 = Array ("Cow", "Dog", "Rat");
Print_r (Array_map ("MyFunction", $a 1, $a 2));
>

Output:

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

Example 3

See what happens when a custom function name is set to null:

<?php
$a 1 = array ("Dog", "Cat");
$a 2 = Array ("Puppy", "Kitten");
Print_r (Array_map (null, $a 1, $a 2));
? >

Output:

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

Articles that you may be interested in

    • PHP Array function array_walk () notes
    • PHP generates a continuous number (letter) Array function range () analysis, PHP lottery program function
    • PHP presses the element to the array header (Array_unshift usage)
    • Use PHP functions in Smarty Templates and how to use multiple functions for a variable in smarty templates
    • The difference between the PHP merge array + and the Array_merge
    • Two-dimensional array removal of duplicate values and Array_unique functions
    • Summary of PHP array functions
    • How do I remove an element from a PHP array (unset,array_splice)?


Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.