Function array_map (): Multi-array callback function-calls the callback function to the unit of the given array.
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: