I have read a lot of related knowledge about PHP array functions on the Internet over the past few days, and I think that Kong's "understanding the new" is true and false. Here is my summary of some experience and I hope to help you.
Arrays in PHP are actually an ordered graph, which is a type that maps values to keys. This type has been optimized in many ways, so you can use it as a real array, or a list (vector), a hash (an implementation of a graph), a dictionary, set, stack, queue, and more possibilities. Because another PHP array can be used as the value, and the tree can be easily simulated. Interpreting these structures is beyond the scope of this manual, but you will find at least one example for each structure. For more information about these structures, we recommend that you refer to external works on this broad topic. Here are some of my PHP array functions:
1. Cut a 1-dimension component into a 2-dimensional array array_chunk ()
- $input_array=array('a','b','c','d','e');
- print_r(array_chunk($input_array,2));
2. Compare two Arrays: array_diff_assoc () or array_diff (). If the returned value is null, the two arrays are the same. Otherwise, the two arrays are different.
3. Use a function to filter the value array_filter () in the array ()
- functionodd($var){
- return($var%2==1);
- }
- functioneven($var){
- return($var%2==0);
- }
- $arrayarray1=array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
- $arrayarray2=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"));
- ?>
4. array_map () applies the callback function to the units of the given array. Its parameters can be an array or multiple arrays, the parameters of the callback function must be the same as those of the callback function.
- // Example of a single parameter, multiply each value in the array by its power 3
- Functioncube ($ n ){
- Return $ n * $ n;
- }
-
- $A=Array(1, 2, 3, 4, 5 );
- $B=Array_map("Cube", $ );
- Print_r ($ B );
- ?>
-
- // Example of multiple array parameters
- Functionshow_Spanish ($ n, $ m ){
- Return "Thenumber $ niscalled $ minSpanish ";
- }
-
- Functionmap_Spanish ($ n, $ m ){
- Returnarray ($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
- // Printoutof $ c
- Array
- (
- [0] =>Thenumber1iscalledunoinSpanish
- [1] =>Thenumber2iscalleddosinSpanish
- [2] =>Thenumber3iscalledtresinSpanish
- [3] =>Thenumber4iscalledcuatroinSpanish
- [4] =>Thenumber5iscalledcincoinSpanish
- )
The above is a summary of PHP array functions.