In java, the index of an array must be a number, and the number of arrays must be determined when the array is declared. Php is more flexible. indexes can make numbers or strings, and the number of arrays can be changed at will.
Objective: to understand the concept of arrays
Learn to traverse arrays (one-and two-dimensional)
Measure the test taker's knowledge about common array operation functions.
1: The index of the array in java must be a number, and the number of arrays must be determined when the array is declared.
Php is more flexible. indexes can make numbers or strings, and the number of arrays can be changed at will.
2: Array concept: an array is a variable (compound variable) that can store a group or a series of values)
3: array classification: index array (index value is a number, starting with 0) and associated array (using string as index value)
4: common assignment methods:
$ Arr [0] = 'a ';
$ Arr [1] = 'BB ';
Or use the array () structure to assign values.
$ Arr = array ('A', 'BB ');
$ Arr = array (1, 3), array (3, 4); two-dimensional
[Note] if the subscript of an array is a string value equivalent to an integer (but cannot start with 0), it is treated as an integer.
For example, $ array [3] and $ array ['3'] reference the same element, $ array ['03'] references another element.
5: Array traversal: ① for loop ② foreach loop ③ list each and while combine to loop
Foreach ($ arr as $ key => $ value)
When foreach starts the loop, the pointer inside the array automatically points to the first unit.
In a loop, the keys and values of the current unit are assigned to $ key and $ value respectively, and the pointer in the array is automatically moved to the next unit.
[Note] foreach operates on an array instead of an array. Therefore, each time foreach starts to execute, the internal pointer of the array automatically points to the first unit,
This means that you do not need to use reset () before the foreach loop ().
Since PHP 5, you can easily add & before $ value to modify the array unit. This method will assign values by reference instead of copying a value.
Each-returns the current (key/value) pair in the array and moves the array pointer one step forward. The key-value pair is returned as an array of four units, with the key names 0, 1, key, and value.
If the internal pointer crosses the end of the array, each () returns FALSE.
After executing each (), the array pointer will stay in the next unit of the array or stay in the last unit when it comes to the end of the array. If you want to use each to traverse the array, you must use reset () to bring the pointer back to the starting position.
List-assign the values in the array to some variables. list () can only be used in the array of numeric indexes and assumes that the numeric index starts from 0.
[Understand] list is a value assignment from right to left.
$ Arr = array ('one', 'two', 'Three, 'Four ');
While (list ($ key, $ val) = each ($ arr )){
Echo "$ key => $ val
";
}
6: Array pointer processing functions
Next-move the internal pointer in the array one by one and return the element value of the next unit. If the array contains empty cells or the unit value is 0, FALSE is returned when this function encounters these cells. We recommend that you use each.
Prev-returns the internal pointer of the array to one position (opposite to next)
End-move the internal pointer of array to the last unit and return its value.
Reset-returns the internal pointer of array to the first unit and returns the value of the first array unit. If the array is empty, FALSE is returned.
Current ($ arr)-returns the current unit in the array (alias: pos) [do not move pointer]
Key ($ arr)-get the key name from the associated array
7: common functions in the array
Count -- (sizeof alias)-calculates the number of units in an array or the number of attributes in an object. int count (mixed $ var [, int $ mode]) $ var is usually array type, all other types have only one unit. The default value of $ mode is 0. 1. enables recursive array counting.
Array_count_values (array $ input)-count the number of occurrences of all values in the array
Array_sum (array $ array)-calculates the sum of all values in the array
Array_unique (array $ array)-removes repeated values from the array [* New array retains original subscript]
Array_keys (array $ input [, mixed $ search_value [, bool $ strict])-returns all key names in the array.
Array_values (array $ input)-return all values in the input array and create a digital index for them. Duplicate values are not processed
In_array (mixed $ needle, array $ haystack [, bool $ strict])-check whether a value exists in the array.
If the needle is a string, the comparison is case sensitive.
If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack.
Array_map (callback $ callback, array $ arr1 [, array $...]) -apply the callback function to the unit of the given array. [note]: if multiple arrays are used, the number of callback function parameters must match the number of arrays.
Array_chunk (array $ input, int $ size [, bool $ preserve_keys])-splits an array into multiple arrays. the number of units of each array is determined by the size. The number of units in the last array may be smaller. The obtained array is a unit in a multi-dimensional array, and its index starts from scratch. $ Preserve_keys retain the original key name in the input array. the default value is false.
Array_merge (array $ array1 [, array $ array2 [, array $...]) combine the elements of one or more arrays. the values of an array are appended to the values of the previous array. Returns an array of results.
Note: If the input array contains the same string key name, the value after the key name overwrites the previous one. 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.
Compare the differences between the addition of $ arr + $ arr2 array and the array_merge () function.
8: Array Functions
Array_change_key_case (array $ input [, int $ case])-returns an array with the string key names in either lower case or upper case. $ Case CASE_UPPER and CASE_LOWER (default)
Array_fill (int $ start_index, int $ num, mixed $ value)-use the value of the value parameter to fill an array with num entries. The key name starts from the value specified by the start_index parameter.
Array_filter (array $ input [, callback $ callback])-use the callback function to filter elements in the array. if the callback function returns TRUE, the current value of the input array is included in the returned result array.
The key name of the array remains unchanged. if the callback function is not provided, array_filter () deletes all elements in the input that are equivalent to FALSE.
Array_walk ()
Array_flip (array $ trans)-swap keys and values in the array
Array_key_exists (mixed $ key, array $ search)-checks whether the given key name or index exists in the array. you can also use isset (). isset () does not return TRUE for NULL values in the array, while array_key_exists () does.
Array_search (mixed $ needle, array $ haystack [, bool $ strict])-Search for a given value in the array. if the value is successful, the corresponding key name is returned.
Array_rand (array $ input [, int $ num_req])-randomly retrieves one or more units from the array.
9: Conversion between arrays and variables
(1) extract (array $ var_array [, int $ extract_type [, string $ prefix])
Use the key name in the var_array array as the variable name and the value as the variable value.
For each key/value pair, a variable is created on the current page and is affected by the extract_type and prefix parameters.
If an array with an operation is indexed, if the key name is converted to a variable, it is all invalid variables (the variable cannot start with a number ), therefore, the second and third parameters must be used.
EXTR_OVERWRITE overwrites existing variables if there is a conflict.
EXTR_SKIP does not overwrite existing variables if there is a conflict.
If EXTR_PREFIX_SAME conflict EXTR_PREFIX_SAME, add prefix before the variable name.
EXTR_PREFIX_ALL adds the prefix to all variable names. Since PHP 4.0.5, this also includes processing digital indexes.
EXTR_PREFIX_INVALID is prefixed only before the variable name of the invalid/number. This mark is newly added to PHP 4.0.5.
EXTR_IF_EXISTS only overwrites the values of variables with the same name in the current symbol table. None of them are processed. It can be used for variables that have defined a combination, and then extract values from an array such as $ _ REQUEST to overwrite these variables. This mark is newly added to PHP 4.2.0.
EXTR_PREFIX_IF_EXISTS creates a variable name with a prefix only when the variable with the same name already exists in the current symbol table. This mark is newly added to PHP 4.2.0.
EXTR_REFS extracts variables as references. This effectively demonstrates that the imported variable still references the value of the var_array parameter. This flag can be used independently OR in extract_type OR with any other flag. This mark is newly added to PHP 4.3.0.
(2) compact (mixed $ varname [, mixed $...]) create an array, including the variable names and their values. if the variable corresponding to the string in the parameter does not exist, ignore it.
10: Conversion between arrays and strings
(1) array explode (string $ separator, string $ string [, int $ limit]) uses a separator character to separate a string.
$ Limit indicates the number of units separated into arrays, and the last unit will contain the rest of the string.
If the limit parameter is negative, all elements except the last limit element are returned. This feature is added in PHP 5.1.0.
(2) string implode (string $ glue, array $ arr) concatenates each unit in the array into a string using a connector.
The positions of $ glue and $ arr are interchangeable.
Join is the alias of implode
11: Array and stack
An array can be used as a stack. the bottom of the stack points to the first element of the array, and the top of the stack points to the last element in the array.
There are two main stack operations: inbound and outbound
(1) inbound stack
Int array_push (array & $ array, mixed $ var [, mixed $...]) -Press one or more units to the end of the array (into the stack ). returned value: the total number of cells in the new array. similar to $ arr [] = $ var; (this method is recommended)
Int array_unshift (array & $ array, mixed $ var [, mixed $...])-insert the incoming unit to the beginning of the array. Note that the units are inserted as a whole, so the incoming units are in the same order. The names of all numeric keys are changed to zero-start counting, and the names of all text keys remain unchanged.
(2) outbound stack
Array_pop (array & $ array)-The last unit of the array is displayed and the length of the array is reduced by one. If array is NULL (or not an array), NULL is returned.
Array_shift (array & $ array)-removes the first unit of array and returns the result. This removes the length of array and moves one of all other units forward. The names of all numeric keys are counted from scratch, and the names of text keys remain unchanged. If array is NULL (or not an array), NULL is returned.
12: sorting of arrays
(1) positive sorting
Sort (array & $ array [, int $ sort_flags])-sorts arrays by values. when this function ends, group units are rescheduled from the lowest to the highest.
Asort (array & $ array [, int $ sort_flags])-sorts the array and maintains the index relationship (other)
Ksort (array & $ array [, int $ sort_flags])-sorts the array by key name and retains the association between key names and data. (This function is mainly used to associate arrays)
(2) reverse sorting
Rsort (array & $ array [, int $ sort_flags])-sorts the array in reverse order. this function sorts the array in reverse order (maximum to lowest ).
Arsort (array & $ array [, int $ sort_flags])-sorts arrays in reverse order and maintains the index relationship.
Krsort (array & $ array [, int $ sort_flags])-sorts the array in reverse order by key name and retains the association between key names and data. It is mainly used to associate arrays.
(3) [understanding] User-defined sorting
Usort (array & $ array, callback $ cmp_function)-sort the values in the array using the user-defined comparison function
Uksort (array & $ array, callback $ cmp_function)-sort the key names in the array using the custom comparison function
Uasort (array & $ array, callback $ cmp_function)-sort the values in the array using the user-defined comparison function and maintain the index Association