Phparray_map () array function instructions

Source: Internet
Author: User
Function array_map (): Multi-array callback function-calls the callback function to the unit of the given array. The code is as follows:
/* Function array_map (): Multi-array callback function-calls the callback function to the unit of the given array.
* 1. Syntax: array array_map (callback, array arr1 [, array...])
* 2. description: return an array containing all the elements in arr1 that have passed the callback function.
* Unit. The number of parameters accepted by callback should be the same as the number of arrays passed to the array_map () function.
* 3. notes:
* 3.1 when the multi-array callback function acts on an array, the key name of the original array is retained, that is, the key name of the returned array is
* Key used for the given array
* 3.2 when multiple arrays return to the function acting on two or more arrays, their lengths must be consistent and
* Key name, which is uniformly assigned a digital index as the key name
*/
// Example of a single array
$ Websites = array ("g" => "google", "B" => "baidu", "y" => "yahoo ");
// Output the original array
Echo"
"; 
print_r($websites);
echo "
";
// Define the callback function for processing a single array
Function change_value ($ value ){
Return ucfirst ($ value). ". com ";
}
$ Urls = array_map ('change _ value', $ websites );
Echo"
"; 
print_r($urls);
echo "
";
// Example of using multiple arrays
$ Arr1 = array (1, 3, 5, 7 );
$ Arr2 = array (2, 4, 6, 8 );
// Define the callback function for processing multiple arrays
Function func1 ($ a, $ B ){
Return $ a * $ B;
}
$ Results = array_map ('function1', $ arr1, $ arr2 );
Echo "returned results after processing multiple arrays using the callback function:
";
Echo"
"; 
print_r($results);
echo "
";

The running effect is as follows:

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.