PHP Array function-array_ map ()

Source: Internet
Author: User
PHP Array function-array_map ()

The Array_map () function returns an array after the user-defined function is functional. Parameters accepted by the callback function

The number should be and the number of arrays passed to the Array_map () function has been.

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

Function: Required. User-defined function name, or null

Array1: Required. Specify array

Array2: Optional. Specify array

ARRAY3: Optional. Specify array

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)

Use multiple parameters:

function myfunction ($v 1,$v 2) {
if ($v 1 = = = $v 2) {
return "Same";
}Else{
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)

When the custom function name is set to null:

$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))

  • 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.