In these few days, I saw the PHP manual. Now I saw the array function, I feel that the "warmth of the old man" is not fake. The following specials have some experience:
Split a 1-dimensional array into 2-dimensional array array_chunk () $ INPUT_ARRAY = Array ('A', 'B', 'C', 'D', 'E'); Print_R (Array_Chunk ($ INPUT_ARRAY, 2); Compare 2 arrays, array_diff_assoc () or array_diff (), if the return value is empty, indicating the same two arrays, otherwise it will be different. Use a function to filter the value array_filter () in the array
Function ODD ($ VAR) {Return ($ VAR% 2 == 1);} Function Even ($ VAR) {Return ($ VAR% 2 == 0);} $ array1 = array ("a" => 1, "B" => 2, "c" => 3, "d" => 4, "e" => 5); $ array2 = array (6, 7, 8, 9, 10, 11, 12); ECHO "ODD: / N"; Print_R (Array_Filter ($ Array1, "ODD")); ECHO "Even: / N"; Print_r (Array_Filter ($ Array2, "Even"));?>>
Array_map () uses the callback function to the unit of a given array, and its parameters can be an array, or multiple arrays, the parameters of the callback function must be the same as that of the call.
// Example of a single parameter, put each value in the array, multiplied by its 3 times
Function Cube ($ n * $ n * $ n; $ a = array (1, 2, 3, 4, 5); $ b = array_map ("cube", $ a); print_r ($ b);?> // Multiple array parameters
Function show_spanish ($ n, $ m) {return "The number $ n IS caled $ m in spanish";} Function map_spanish ($ n, $ m) {Return Array ($ n => $ m);} $ a = Array (1, 2, 3, 4, 5); $ b = array ("uno", "dos", "tres", "cuatro", "cinco"); $ c = array_map ("show_spanish", $ A $ b); Print_R ($ C); $ d = array_map ("map_spanish", $ A, $ b); Print_R ($ d);?> // Output result //printout of $ carray ([0] = > The Number 1 Is Called Uno in Spanish [1] => The Number 2 IS Called Dos in Spanish [2] => The Number 3 Is Called Tres in Spanish [3] => The Number 4 IS Called Cuatro in Spanish [4 ] => The number 5 is called Cinco in spanish) Array_multisort () Sort multiple arrays or multi-dimensional arraments can be used to sort multiple arrays at a time or sorted according to a certain dimensional array. When you are sorted, the original key name association is retained, and the functionality of the implementation and the ORDER BY in SQL are almost.
$ AR1 = Array ("10", 100, 100, "a"); $ ar2 = array (1, 3, "2", 1); Array_Multisort ($ AR1, $ AR2);?>
// Return the result (maintain the association of the original array) $ AR1 = array ([0] => 10 [1] => a [2] => 100 [3] => 100) $ ar2 = array ([0 ] => 1 [1] => 1 [2] => 2 [3] => 3)
/ / Realize students' performance rankings
$ grade = array ("score" => Array (70, 95, 70.0, 60, "70"), "name" => Array ("ZHANG SAN", "Li Si", "Wang Wu", "ZHAO Liu "," Liu qi ")); array_multisort ($ grade [" score ", sort_numeric, sort_desc, // uses the score as a value, from high to low sort $ grade [" name "], sort_string, sort_asc); // Put the name as a string, by small to large sort?> // output result
ARRAY
'score' =>
ARRAY
0 => 95,
1 => '70', 2 => 70,
3 => 70,
4 => 60,
),
'Name' =>
ARRAY
0 => 'li si',
1 => 'liu qi',
2 => 'WANG Wu',
3 => 'zhang san',
4 => 'zhao liu',
),
)
Array_rand () and shuffle (), one is randomly removed from the array, and the other is chaos in the order of the values in the array, and then takes the value from the quarter, which is randomly removed from the array. The functionality of the data can be used to implement features such as lottery.
Array_reduce () uses the callback function function to itself into each unit in the INPUT array to simplify the array into a single value. If an optional parameter initial is specified, this parameter will be treated as a first value in an array, or if an array is empty as a final return value. PHP
Function RSUM ($ V, $ W) {$ V = $ W; RETURN $ V;} Function Rmul ($ V, $ W) {$ V * = $ W; Return $ V; $ A = Array (1 , 2, 3, 4, 5); $ x = array (); // implement array_sum () function $ b = array_reduce ($ A, "rsum"); // 15 = 1 2 3 4 5 $ c = array_reduce ($ A, "Rmul", 10); // 1200 = 1 * 2 * 3 * 4 * 5 * 10 $ d = array_reduce ($ x, "rsum", 1); // 1?> This function can realize the sum (product, difference function)
Array Array_Reverse (Array Array [, Bool Preserve_Keys] returns an array of unit sequence, if preserve_keys = true, retain the original key name, which is implemented in the functionality of the Order by DESC in SQL. Functional functions are: array_multisort (), sort (), etc.
Array_Search () Search for a given value in an array, if success, return the corresponding key name and the other function of the function: array_key_exists (), in_ARRAY ()
Array_push () Press one or more units into the end of the array (in the stack) Array_POP () to pop up the array first unit (out of the stack) array_shift () out of the array, array_unshift () inserting one or inserting one in an array Multiple units
Array_slice () takes a segment from the array, as a return value
Array_SPLICE () removes part of the array and replaces other values, can be used to change the value of a (several) item in the array
Array_Values () Returns all values in the array and establishes a digital index
Array_Keys () Returns all key names in the array
Array_walk ()
Apply user-defined functions for each member of the array to add a prefix to the data in the array, change the case,
Various operations such as numerical calculations.
The functions that use user-defined functions to process arrays:
Array_map (), Array_Reduce (), array_filter ()
PHP
$ fruits = array ("d" => "lemon", "a" => "Orange", "B" => "banana", "c" => "apple"); Function Test_alter (& $ ITEM1, $ KEY, $ Prefix {$ ITEM1 = $ Prefix: $ ITEM1 ";} Function Test_Print ($ ITEM2, $ Key) {Echo" $ Key. $ item2 / n ";} echo" before ...: / n " Array_walk ($ fruits, 'test_print'); array_walk ($ fruits, 'test_alter', 'fruit'); echo "... and after: / n"; array_walk ($ fruits, 'test_print');?>>>>>>>>>>>>>>>>>>>
Arsort () reverse sorting the array and retains an index relationship
ASORT () Sort and maintain indexing relationships for arrays
Rsort () retrograde the array, does not retain index relationship
sort () sortes the array, not reserved index relationship
Ksort () Regard the array to sort in the key name
Uksort () Sort by using user-defined comparison functions to the key names in the array
Usort () Sort by using user-defined comparison functions to the values in the array
UASORT () uses user-defined comparison functions to sort the values in array and keep an index association
Natsort () Sort by "Natural Sort" algorithm
NatcaseSort () uses the "Natural Sort" algorithm to do not distinguish between uncounting letters
Function CMP ($ A, $ B) {IF ($ A == $ B) Return 0; Return ($ A> $ b)? -1: 1;} $ a = array (4 => "FOUR", 3 => "Three", 20 => "TWENTY", 10 => "Ten"); uksort ($ A, "CMP"); while (list ($ key, $ value) = Each ($ a)) {echo "$ key: $ value / n";}?>
Compact () creates an array, including variable names and their values to find the variable name in the current symbol table and add it to an array of outputs, the variable name is the value of the change in the variable, the value of the value of the key EXTRACT () This function is used to import variables into the current symbol table from an array.