PHP Array Function array_map () Notes

Source: Internet
Author: User
Tags smarty template

PHP Array Function array_map () Notes

Definition and usage

The array_map () function returns the array after the User-Defined Function is applied. The number of parameters accepted by the callback function should be the same as the number of arrays passed to the array_map () function.

Syntax

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

Parameters Description
Function Required. The name of the User-Defined Function, or null.
Array1 Required. Specified array.
Array2 Optional. Specified array.
Array3 Optional. Specified array.

Example 1

<?phpfunction 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:

<?phpfunction 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));?>

Output:

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

Example 3

See 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));?>

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 numbers (letters) Array Function range () analysis, PHP lottery program function
  • Php pushes an element to the array header (array_unshift usage)
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Php uses the filter function to verify the mailbox, url, and IP address
  • PHP merges arrays + and array_merge
  • Introduction to array union, intersection, and difference set functions in php
  • Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())

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.