Define and traverse the array of PHP learning notes. An array in php is a special data type. it can be called an object or a memory, which can store other data types in php, for example, an array in php may contain arrays that are of a special data type, which can be an object or a memory, which can store other data types in php, for example, an array may contain numeric, numeric, and objects. Next, let's take a look at the definition and traversal of php arrays.
① Method 1 for creating arrays
$ Arr [0] = 123;
$ Arr [1] = 90;
....
Concept:
[0]-> This is called a subscript or a keyword.
$ Arr [0]-> This is an element of an array.
$ Arr [0] = 123; 123 indicates the value of the $ arr [0] element.
$ Arr-"is the name of the array.
☞In the php array, the value stored by an element can be of any data type.
② Method 2 for creating an array
Basic syntax
$ Array name = array (value 1, value 2 ,.......);
Example:
$ Arr = array (89.5, "helllo );
③ Method 3: Create an array(By default, the subscript of our element is numbered from 0, but you can also specify it yourself)
Basic syntax $ arr ['logo '] = "Beijing ";
$ Arr ['Shanghai'] = 123;
....
Or
$ Arr = array ("logo" => "Beijing", "MK" => 123,4 => 678 );
Array traversal method:
Note: If you use for while do... while, make sure that the subscript of the array is discharged sequentially from 0.
Number of elements in the array. you can use the system function count.
//
The code is as follows: |
|
For loop traversal method For ($ I = 0; $ I Echo' '. $ Arr [$ I]; } // While loop traversal method $ I = 0; // cyclic control variable While ($ I Echo" ". $ Colors [$ I]; $ I ++; } // Do... while $ I = 0; // cyclic control variable Do { Echo" ". $ Colors [$ I]; $ I ++; } While ($ I // Foreach traversal method This foreach applies to a wider range of applications. Foreach ($ arr as $ key => $ val ){ Echo $ key. "=". $ val ." "; } |
Array-related functions of php
① Count function
The basic usage is
Count ($ array name); count the total number of elements in the array.
② Is_array
③ Print_r and var_dump
④ Explode separates strings
Case:
$ Str = "Zhejiang & Taizhou & Hangzhou ";
// You can consider splitting strings during actual development.
$ Arr = explode ("&", $ str );
Print_r ($ arr );
Array search
Example:
The code is as follows: |
|
$ Arr_a = array (0 => "a", 1 => "B", 2 => "c "); $ Key = array_search ("a", $ arr_a ); If ($ key! = FALSE ){ Echo "key name: $ key "; } Else { Echo 'no matching result '; } ?> |
The output result is as follows:
Key name: 0
Array_key_exists () function
If a specified key is found in an array, the array_key_exists () function returns true; otherwise, false. The format is as follows:
Boolean array_key_exists (mixed key, array );
The following example searches for apple in the array key. if it is found, the color of the fruit is output:
The code is as follows: |
|
$ Fruit ["apple"] = "red "; $ Fruit ["banana"] = "yellow "; $ Fruit ["pear"] = "green "; If (array_key_exists ("apple", $ fruit )){ Printf ("apple's color is % s", $ fruit ["apple"]); } |
The result of executing this code is as follows:
Apple's color is red
Merge arrays
The array_merge () function combines arrays and returns a Union array. The obtained array starts with the first input array parameter and is immediately added according to the order in which the following array parameters appear. The format is:
Php code
Array array_merge (array array1 array2 ..., ArrayN)
This function combines the units of one or more arrays. the values of an array are appended to the values of the previous array. Returns an array of results.
If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.
If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.
The code is as follows: |
|
$ Fruits = array ("apple", "banana", "pear "); $ Numbered = array ("1", "2", "3 "); $ Cards = array_merge ($ fruits, $ numbered ); Print_r ($ cards ); // Output // Array ([0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3) ?> |
2. append an array
The array_merge_recursive () function is the same as the array_merge () function. two or more numbers can be combined to form a combined array. the difference between the two is that when a key in an input array already exists in the result array, the function will adopt different processing methods. array_merge () overwrites the existing key/value pairs and replaces them with the key/value pairs in the current input array. array_merge_recursive () merges the two values, form a new array and use the original key as the array name. Another form of array merging is recursively appending an array. The format is:
Php code
Array array_merge_recursive (array array1, array array2 [..., Array arrayN])
The program example is as follows:
The code is as follows: |
|
$ Fruit1 = array ("apple" => "red", "banana" => "yellow "); $ Fruit2 = array ("pear" => "yellow", "apple" => "green "); $ Result = array_merge_recursive ($ fruit1, $ fruit2 ); Print_r ($ result ); // Output // Array ([apple] => Array ([0] => red [1] => green) [banana] => yellow [pear] => yellow) ?> |
Now, the key apple points to an array consisting of two color values.
3. connect to an array
The array_combine () function returns a new array consisting of a set of submitted keys and corresponding values. The format is:
Array array_combine (array keys, array values)
Note: the two input arrays must be of the same size and cannot be empty. Example:
The code is as follows: |
|
$ Name = array ("apple", "banana", "orange "); $ Color = array ("red", "yellow", "orange "); $ Fruit = array_combine ($ name, $ color ); Print_r ($ fruit );
// Output // Array ([apple] => red [banana] => yellow [orange] => orange) ?> |
4. split the array array_slice ()
The array_slice () function returns a part of the array, starting from the key offset and ending at the offset + length position. Form:
Php code
Array array_slice (array, int offset [, int length])
When offset is a positive value, the split starts from the offset position starting from the array. if offset is a negative value, the split starts from the offset position at the end of the array. If the optional length parameter is omitted, the split starts from offset to the last element of the array. If length is given and it is a positive number, it will end at the offset + length position starting with the array. On the contrary, if length is given and it is a negative number, it ends at the count (input_array)-| length | position starting with the array. Consider an example:
The code is as follows: |
|
$ Fruits = array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon "); $ Subset = array_slice ($ fruits, 3 ); Print_r ($ subset ); // Output // Array ([0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon) ?> |
Array sorting (the following method is used for one-dimensional arrays)
• The sort () function is used to sort array units from low to high.
• Rsort () is used to sort array units from high to low.
• The asort () function is used to sort array units from low to high and maintain the index relationship.
• Arsort () is used to sort array units from high to low and maintain the index relationship.
• The ksort () function is used to sort array cells by key names from low to high.
• The krsort () function is used to sort array cells by key names from high to low.
Multi-dimensional array sorting
For example, most of them are arrays:
The code is as follows: |
|
$ Arr = array ( 'D' => array ('id' => 5, 'name' => 1, 'age' => 7 ), 'B' => array ('id' => 2, 'name' => 3, 'age' => 4 ), 'A' => array ('id' => 8, 'name' => 10, 'age' => 5 ), 'C' => array ('id' => 1, 'name' => 2, 'age' => 2) ); |
Sort age items in a two-dimensional array.
You need to use the PHP built-in function array_multisort (). you can refer to the manual.
Custom functions:
The code is as follows: |
|
Function multi_array_sort ($ multi_array, $ sort_key, $ sort = SORT_ASC ){ If (is_array ($ multi_array )){ Foreach ($ multi_array as $ row_array ){ If (is_array ($ row_array )){ $ Key_array [] = $ row_array [$ sort_key]; } Else { Return false; } } } Else { Return false; } Array_multisort ($ key_array, $ sort, $ multi_array ); Return $ multi_array; } // Process Echo" "; Print_r (multi_array_sort ($ arr, 'age'); exit;// Output Array ( [C] => Array ( [Id] => 1 [Name] => 2 [Age] => 2 ) [B] => Array ( [Id] => 2 [Name] => 3 [Age] => 4 ) [A] => Array ( [Id] => 8 [Name] => 10 [Age] => 5 ) [D] => Array ( [Id] => 5 [Name] => 1 [Age] => 7 ) ) |
Written by Daewoo
Other, which can store other data types in php. for example, an array can contain struct...