Comparison of use of Array_map and Array_walk in PHP

Source: Internet
Author: User

First, Array_map ()

1 array_map() . The function functions the user-defined function to each value in the array, and returns an array with the new value after the function of the user-defined function, and if there is no return value after functions, the corresponding new value array is empty.

2. The number of arguments that the callback function accepts should be the same as the number of arrays passed to array_map() the function.

3. Tip: You can enter one or more arrays into the function.

If the phase function input 2 arrays, its function accepts the parameter also should have two, the map gives the function to pass the value, is each time from the two array each one transmits the letter
Number. That is, multiple arrays are committed synchronously, not after committing an array and committing the next commit several arrays, the function also needs to have several parameters

4. Syntax:array array_map ( callable callback,arrayarr1 [, array $… ] )

Instance:

<?php //单数组形式function myfunction($v){ if ($v==="Dog") {  return "Fido"; } return $v;}$a=array("Horse","Dog","Cat");print_r(array_map("myfunction",$a));//多数组形式function myfunction1($v1,$v2){ if ($v1===$v2) {  return "same"; } return "different";}$a1=array("Horse","Dog","Cat");$a2=array("Cow","Dog","Rat");print_r(array_map("myfunction1",$a1,$a2));?>

Comparison of use of Array_map and Array_walk in PHP

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.