Function call, function structure at a glance anonymous functions, real-time use, high security This article mainly uses callback functions to process arrays, two ways:
Function call, function structure at a glance
Anonymous functions, instant use, high security
The following shows how to use several functions: mainly understanding how to use callback functions:
Count () calculates the number of units in the array or the number of attributes in the object.
Array_count_values () counts the number of occurrences of all values in the array.
Array_unique () removes repeated values from the array
Array_filter () uses the callback function to filter elements in the array.
Array_walk () calls back each element in the array using a user-defined function.
Array_map () is used as the callback function for each element of the array.
Use array_filter () and array_walk () to focus on the usage of callback functions, especially anonymous functions with high security.
"Linux", "webserver" => "Apache", "db" => "MySQL", "language" => "PHP"); echo '---------- print_r ()----------
'; Print_r ($ lamp); // print the result: Array ([OS] => Linux [webserver] => Apache [db] => MySQL [language] => PHP) echo'
';/*** Count () calculates the number of units in the array or the number of attributes in the object */echo' ---------- count () string ----------
'; $ Str = "hello world"; $ str2 = ""; var_dump (count ($ str); // print the result: int (1) echo'
'; Var_dump (count ($ str2); // print the result: int (1) echo'
'; Echo' ---------- count () array ----------
'; Echo count ($ lamp); // print the result: 4 echo'
'; Echo' ---------- count () multi-dimensional array ----------
'; $ Web = array ("lamp" => array ("OS" => "Linux", "webserver" => "Apache ", "db" => "MySQL", "language" => "PHP"), "lamp2" => array ("OS" => "Linux ", "webserver" => "Apache", "db" => "MySQL", "language" => "PHP "), "lamp3" => array ("OS" => "Linux", "webserver" => "Apache", "db" => "MySQL ", "language" => "PHP"); echo count ($ web); // print the result: 3 echo'
'; Echo count ($ web, 1); // print the result: 15. the array has three elements and the child array has 12 elements echo'
';/*** Array_count_values () count the number of times all values in the array appear */echo' ---------- array_count_values ()----------
'; $ Lamp4 = array ("OS" => "Linux", "webserver" => "Apache", "db" => "MySQL ", "db2" => "MySQL", "language" => "PHP"); echo print_r (array_count_values ($ lamp4); // print the result: array ([Linux] => 1 [Apache] => 1 [MySQL] => 2 [PHP] => 1) 1 echo'
';/*** Array_unique () removes repeated values in the array * the value that appears for the first time retains its key, and the other values are removed */echo' ---------- array_unique ()----------
'; $ Lamp5 = array ("OS" => "Linux", "webserver" => "Apache", "db" => "MySQL ", "db2" => "MySQL", "language" => "PHP"); echo print_r (array_unique ($ lamp5); // print the result: array ([OS] => Linux [webserver] => Apache [db] => MySQL [language] => PHP) 1 echo'
';/*** Array_filter () use the callback function to filter the elements in the array */echo' ---------- array_filter ()----------
'; $ Arr = array (1, 2, false,-3, null,-2, 3, 4, "", 5,-5,-4,-1); echo''; Var_dump ($ arr);/* print the result: array (13) {[0] => int (1) [1] => int (2) [2] => bool (false) [3] => int (-3) [4] => NULL [5] => int (-2) [6] => int (3) [7] => int (4) [8] => string (0) "" [9] => int (5) [10] => int (-5) [11] => int (-4) [12] => int (-1)} */echo '---------- array_filter () call without passing parameters ----------
'; Var_dump (array_filter ($ arr);/* print the result: the result contains false, null, and "". the array (10) is filtered out) {[0] => int (1) [1] => int (2) [3] => int (-3) [5] => int (-2) [6] => int (3) [7] => int (4) [9] => int (5) [10] => int (-5) [11] => int (-4) [12] => int (-1)} */echo '---------- array_filter () filter using custom functions ----------
'; Function myfun ($ value) {// A self-written function. if it is greater than or equal to 0, true is returned. otherwise, false if ($ value >=0) {return true;} is returned ;} else {return false ;}} var_dump (array_filter ($ arr, "myfun");/* print the result: if the value is smaller than 0, the array (8) is filtered out) {[0] => int (1) [1] => int (2) [2] => bool (false) [4] => NULL [6] => int (3) [7] => int (4) [8] => string (0) "" [9] => int (5)} */echo '---------- array_filter () filter through anonymous functions ----------
'; Echo' ---------- It is recommended that you use an anonymous function if you do not need to call this function elsewhere. it is convenient and secure ----------
'; Var_dump (array_filter ($ arr, function ($ value) {return! ($ Value % 2 = 0) ;});/* print the result: the multiple of 2 in the result. the array (6) is filtered out) {[0] => int (1) [3] => int (-3) [6] => int (3) [9] => int (5) [10] => int (-5) [12] => int (-1)} * // *** array_walk () use a user-defined function to perform callback processing on each element in the array */echo '---------- array_walk ()----------
'; $ Arr2 = array (1, 2, 3, 4, 5); print_r ($ arr2); echo'
';/* Print the result: array prototype print Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) */echo '---------- array_walk () does not reference $ value to call the function ----------
'; Function myfun2 ($ value) {// $ value = $ value * $ value;} array_walk ($ arr2, "myfun2") is not referenced "); // $ value print_r ($ arr2); echo 'is not referenced'
';/* Print the result: Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) */echo '---------- array_walk () has a reference & $ value to call the function ----------
'; Function myfun3 (& $ value) {// Reference & $ value = $ value * $ value;} array_walk ($ arr2, "myfun3 "); // Reference & $ value print_r ($ arr2); echo'
';/* Print the result: Array ([0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25) */echo '---------- array_walk () simultaneously processes the key and value ----------
'; $ Arr3 = array ("one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5); print_r ($ arr3); echo'
';/* Print the result: array prototype print Array ([one] => 1 [two] => 2 [three] => 3 [four] => 4 [five] => 5) */echo '---------- The array_walk () call function has two parameters, namely, the key and value operation ---------- at the same time ----------
'; Function myfun4 (& $ value, $ key) {$ value = $ value * $ value; echo $ key ."~~~~~ ". $ Value ."
";} Array_walk ($ arr3," myfun4 ");/* print the result: one ~~~~~ 1 two ~~~~~ 4 three ~~~~~ 9 four ~~~~~ 16 five ~~~~~ 25 */echo '---------- The array_walk () call function has two parameters, namely, operate the anonymous function ---------- on both the key and value ----------
'; Array_walk ($ arr3, function ($ value, $ key) {echo $ key ."~~~~~ ". $ Value ."
";});/* Print the result: one ~~~~~ 1 two ~~~~~ 4 three ~~~~~ 9 four ~~~~~ 16 five ~~~~~ 25 * // *** array_map () is used as the callback function for each element of the array */echo '---------- array_map ()----------
'; $ Arr4 = array (1, 2, 3, 4, 5); $ arr5 = array (6, 7, 8, 9, 0); print_r ($ arr4); echo'
';/* Print the result: array prototype print Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) */echo '---------- array_map () 1 ----------
'; Function myfun5 ($ value) {return $ value * $ value;} $ arr4new = array_map ("myfun5", $ arr4); print_r ($ arr4new ); echo'
';/* Print the result: Array ([0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125) */echo '---------- array_map () 2 pay attention to return ----------
'; Function myfun6 ($ value) {echo $ value * $ value ."
";}$ Arr4new = array_map (" myfun6 ", $ arr4); print_r ($ arr4new); echo'
';/* Print the result: the value in the array is gone, because there is no return in the function, so array_map () is used () note: return 1 8 27 64 125 Array ([0] => [1] => [2] => [3] => [4] =>) */echo '---------- array_map () 3 ----------
'; Function myfun7 ($ value, $ value2) {echo "$ value ~~~~~ $ Value2
"; Return 1 ;}$ arr45new = array_map (" myfun7 ", $ arr4, $ arr5); print_r ($ arr45new); echo'
';/* Print the result: 1 ~~~~~ 6 2 ~~~~~ 7 ~~~~~ 8 4 ~~~~~ 9 5 ~~~~~ 0 Array ([0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1) */echo '---------- array_map () 4 ----------
'; $ Arr45new = array_map (null, $ arr4, $ arr5); print_r ($ arr45new); echo'
';/* Print the result: Array ([0] => Array ([0] => 1 [1] => 6) [1] => Array ([0] => 2 [1] => 7) [2] => Array ([0] => 3 [1] => 8) [3] => Array ([0] => 4 [1] => 9) [4] => Array ([0] => 5 [1] => 0) */echo '---------- array_map () 5 ----------
'; $ Arr6 = array (1, 2, 3, 4, 5); $ arr7 = array ("one", "two", "three "); $ arr8 = array ("aa", "bb", "cc", "dd"); $ arr678new = array_map (null, $ arr6, $ arr7, $ arr8 ); print_r ($ arr678new); echo'
';/* Print the result: Array ([0] => Array ([0] => 1 [1] => one [2] => aa) [1] => Array ([0] => 2 [1] => two [2] => bb) [2] => Array ([0] => 3 [1] => three [2] => cc) [3] => Array ([0] => 4 [1] => [2] => dd) [4] => Array ([0] => 5 [1] => [2] => ))*/
The above is the PHP development (24) for Android programmers-array operation-related functions (2) callback functions-PhpStorm content. For more information, see PHP Chinese network (www.php1.cn )!