**First, create a new array**The array has no size limit, so you can create an array simply by creating a reference, for example: $state [0]= ' 111 '; $state [1]= ' 222 '; If the index value is a numeric index and is incremented, the index value can also be omitted at creation time. $state [] = ' 111 '; $state [] = ' 222 '; Arrays can be created using array (), such as: $state =array (' 111 ', ' 222 '), $state =array (' one ' + ' 111 ', ' one ' = ' 222 '); Use list () to extract an array, for example: $state = Array (' One ', ' two ', ' three '), List ($one, $two, $three) = $state; The three values in the array are assigned to $one, $two, $three, can be output directly.

**populate the array with predefined value ranges, for example:**$num = Range (1, 6);//similar to $num =array (1, 2, 3, 4, 5, 6); selectable parity: $num =range (1, 6, 2); $num = Array (1, 3, 5), selectable letters: $ch =range ("A", "F"),//$ch = Array ("A", "B", "C", "D", "E", "F");

**second, the output array**The most common way to output array content is to iterate through the individual array keys and output the corresponding values.the

**foreach ()** function can easily traverse an array to get each value and key. The Print_r () function makes it easy to output the contents of an array to the screen. The Prinf_r () function takes a variable and sends its contents to the standard output, returning true on success, or false.

**var_dump ();**Detailed printing of array information;

**three, test array**You know whether a particular variable is an array by using the Is_array () function.The

**is_array ()** function determines whether a variable is an array, or returns True if it is, otherwise false. Even if the array contains only one value, it will be considered an array.

**Iv. Adding and removing array elements**PHP provides a number of functions for enlarging and shrinking arrays, which facilitates the simulation of various queue implementations.

**to add an element to the array header:**the

**array_unshift ()** function adds elements to the array header. All existing numeric keys are modified accordingly to reflect their new position in the array, but the association keys are not affected. $state = Array (' One ', ' n ', ' Array_unshift '); ($state, ' xx ');//$state = array (' 00 ', ' 11 ', ' 22 ', ' 33 ');

**add an element at the end of the array:**The

**array_push ()** function adds the value to the end of the array and returns the total number of elements in the array after the new value is added. At the same time, by passing this variable as an input parameter to this function, the array is pressed into multiple variables (elements) in the form of:$state = Array (' 1 ', ' 2 '); Array_push ($state, ' 3 ');//$state = Array (' 1 ', ' 2 ', ' 3 ');

**To ****Delete an element from an array header:**the

**array_shift ()** function deletes and returns the first element found in the array. As a result, if you are using a numeric key, all corresponding values are moved down, and the array using the associated key is not affected in the form of:$state = Array (' 1 ', ' 2 '); Array_shift ($state);//$state = Array (' 2 ');

**To ****Delete an element from the end of an array:**The

**array_pop ()** function deletes and returns the last element of the array, in the form of:$state = Array (' 1 ', ' 2 '); Array_pop ($state);//$state = Array (' 1 ');

**Five, positioning array elements**
**To ****search for an array:**The

**in_array ()** function searches the array for a specific value, returns True if the value is found, or false, in the form of the following:$user = Array (' 111 ', ' 222 ', ' 333 ');$str = ' 111 ';if (In_array ($str, $user)) {echo ' yes ';}else{echo ' no ';}The third parameter, strict, is optional, forcing In_array () to consider the type when searching.

**search for associative arrays:**If you are in an arrayThe function array_key_exists () returns True if a specified key is found, otherwise false is returned. The form is as follows:$arr = Array (' One ' = ' 1 ', ' one ' = ' 2 ', ' three ' = ' 3 ');

**array_key_exists**(' both ', $arr)) {echo ' Yes ';} else{echo ' no ';}

**search for associative arrays:**The

**array_search ()** function searches for a specified value in an array, returns the corresponding value if found, otherwise returns false, in the form of the following: $arr = Array (' One ' = ' 1 ', ' one ' = ' 2 ', ' three ' = ' 3 '), if (Array_search (' 1 ', $arr)) {echo ' Yes ';} else{echo ' no ';}

**get array key:**The

**array_key ()** function returns an array that contains all the keys found in the searched array in the following form:$arr = Array (' One ' = ' 1 ', ' one ' + ' 2 ', ' three ' = ' 3 '); $keys = Array_keys ($arr); Var_dump ($keys);//array (3) {[0]=> string (3) "One" [1]=> string (3) "One" [2]=>string (5) "three"}

**Get array Value:**The

**array_values ()** function returns all the values in an array and automatically provides an array index for the returned array, in the form of the following:$arr = Array (' One ' = ' 1 ', ' one ' + ' 2 ', ' three ' = ' 3 '); $values = Array_values ($arr); Var_dump ($values);//array (3) {[0]=> string (1) "1" [1]=> string (1) "2" [2]=>string (1) "3"}

**Vi. iterating through an array**Typically, you need to iterate through the array and get the keys or values (or both), and PHP provides some functions to meet the requirements. Many functions can accomplish two tasks, not only to get the key or value of the current pointer position, but also to move the pointer down an appropriate position.

**gets the current array key:**The key

**()** function returns the keys in the array where the current pointer is located, in the form of the following:$arr = Array (' One ' = ' 1 ', ' one ' + ' 2 ', ' three ' = ' 3 '); $key = key ($arr); Var_dump ($key);//string (3) "One" Note: Each time you call key (), the pointer is not moved.

**gets the current array value:**the current

**()** function returns the array value in the array where the pointer is currently located. The form is as follows:$arr = Array (' One ' = ' 1 ', ' one ' + ' 2 ', ' three ' = ' 3 '); $value = current ($arr); Var_dump ($value);//string (1) "1"

**gets the current array key and value:**The each

**() function** returns the current key/value pair of the array and advances the pointer to a position in the following form:$arr =array (' one ' = ' 1 ', ' one ' + ' 2 ', ' three ' = ' 3 ');Var_dump (each ($arr));//array (4) {[1]=> string (1) "1" ["Value"]=> string (1) "1" [0]=> string (3) "One" ["Key"]=>string (3) "One" [ "Key"]=>string (3) "One"}The returned array contains 4 keys, the key 0 and key keys contain the key names, and key 1 and value contain the corresponding data. Returns False if each () is executed before the pointer is at the end of the array.

**seven, moving array pointers**
** **
**1) Move the pointer to the next array position**The

**next ()** function returns the array value immediately next to the current array pointer, or False if the pointer is in the last position of the array.$arr = Array (' One ' = ' 1 ', ' both ' = ' 2 ', ' three ' = ' 3 '), Echo next ($arr);//Output 2echo next ($arr);//Output 3

**2) Move the pointer to the previous array position**The

**prev () function** returns the array value that is located in the previous position of the current pointer and returns False if the pointer is in the first position of the array.

**3) Move the pointer to the first array position**The

**Reset () function** is used to set the array pointer back to the beginning of the array, and if you need to view or manipulate an array multiple times in a script, this function is often used, and this function is often used at the end of a sort.

**4) Move the pointer to the last array position** The

**end () function** moves the pointer to the last position of the array and returns the last element. To pass an array value to a function:The

**array_walk () function** passes individual elements in an array to a user-defined function. This function will work if you need to complete a specific action on each array element. The form is as follows:function test ($value, $key) {echo$value. ' '. $key. ';} $arr = Array (' One ' = ' 1 ', ' one ' + ' 2 ', ' three ' = ' 3 '); Array_walk ($arr, ' test ');//1 ONE//2 TWO//3 Three If you use a multidimensional array, The Array_walk_recursive () function introduced in PHP5 can recursively apply a user-defined function to each element of an array.

**Viii. Determining array size and element uniqueness**
**determine the size of the array:**the

**count () function** returns the total number of values in the arrayIf the optional mode parameter (set to 1) is activated, the array will be recursive counting statistics elements. Count (Array (), 1);Note: the sizeof () function is the alias of Count (). Functionally consistent.

**statistics the frequency at which array elements appear:**
**array_count_values () function**Returns an array that contains the associated key/value pairs, in the form of the following:$arr = Array (' A ', ' B ', ' C ', ' a ');$res = array_count_values ($arr);Print_r ($res);//array ([A] = 2 [B] = 1 [C] = 1)The value indicates how often it occurs.

**to determine a unique array element:**The Array_unique () function removes all duplicate values in the array, returning an array of unique values, in the form of the following:$arr = Array (' A ', ' B ', ' C ', ' a ');$res = Array_unique ($arr); Print_r ($res);//array ([0] = A [1] = B [2] = = C)The optional parameter sort_flags (new in PHP5.2.9) determines how the array keys are sorted. By default, values are sorted as strings, but they can also be sorted by numeric values (Sort_numeric), or by using the PHP default sorting method (Sort_regular), and also by the localized environment (sort_locale_string).

**Nine, array sorting**
** **
**Reverse array element ordering**The

**array_reverse () function** resets the order of the elements in the array in the following form:$arr = Array (' A ', ' B ', ' C ');$res = Array_reverse ($arr);Print_r ($res);//array ([0] = C [1] = B [2] = A)If the optional parameter Preserve_keys is set to true, the key mapping is maintained, otherwise the values of the re-placement correspond to the corresponding keys at the previous position. $arr = Array (' A ', ' B ', ' C '); $res = Array_reverse ($arr, True);p Rint_r ($res);//array ([2] = C [1] = b [0] = A)

**permutation array keys and values:**The

**array_flip () function** replaces the role of the key and its corresponding value in the array in the form of the following:$arr = Array (' A ', ' B ', ' C '), $res = Array_flip ($arr);p Rint_r ($res),//array ([A] = 0 [B] = 1 [c] + 2)

**Array Sorting**The sort

**() function** sorts the array by the order of the values from lowest to highest, in the form of the following:The sort () function does not return the sorted array, but instead it simply "in place" sorts the arrays, regardless of the result, no value is returned. The sort_flags parameter is optional and modifies the default behavior of the function based on the value specified by this parameter. 1) sort_numeric, sorted by value. is useful for sorting integers and floating-point numbers. 2) sort_regular, sort the elements according to the corresponding ASCII values. 3) sort_string, sort the elements in the correct order close to what people perceive. The Asort () function is required to keep the array ordering under the initial key/value corresponding condition.

**assort () function**The same as the sort () function, sorted in ascending order, except that it retains the key/value association in the form of: $arr = Array (' C ', ' B ', ' A '); $res = $arr;p rint_r ($arr); sort ($arr);p Rint_r ($ ARR); Asort ($res);p Rint_r ($res),//array ([0]=> c [1] = b [2] = a) Array ([0] = a [1] = B [2] + C ) Array ([2] = A [1] = B [0] = = C)

**sort an array in reverse order**The

**rsort () function** is the same as the sort () function, except that it sorts the array elements in reverse order (descending). If you use the optional sort_flags parameter, the specific sort behavior is determined by this value. In the case of a key/value pair, the array is sorted in reverse order with asort ().the

**arsort () function** keeps the key/value association, but it sets the array up in reverse order$arr = Array (' C ', ' B ', ' A ', ' D '); Arsort ($arr); Print_r ($arr);//array ([3] = D [0] = C [1] = = B [2] + A) If the optional sort_flags parameter is used, the specific sort behavior will be determined by this value.

**Array Natural Sorting**the

**natsort () function** provides a sort mechanism that is equivalent to people's usual use. The typical algorithms are sorted as follows: P1.jpg,p10.jpg, p2.jpg, p20.jpg use the Natsort () function as follows: P1.jpg,p2.jpg, p10.jpg, p20.jpg the natural sort of case insensitive such as: Picture1.jpg, Picture10.jpg, picture2.jpg, picture20.jpg key value array sort Ksort () function keys array Sort, if successful returns true, the failure will return false to sort the array keys in reverse order Krsort () The operation of the function is the same as Ksort (), and the keys are sorted in reverse order. Sort by user-defined rule the Usort () function provides an alternative sorting method that can be sorted by using the user-defined comparison algorithm specified in the function. This function is required if you need to sort the data in some way, and any built-in sort functions for PHP do not provide support.

**10. Array merging, splitting, joining and decomposition**
**Merging Arrays**The

**array_merge () function** merges the arrays together to return an array of unions. The resulting array starts with the first input array parameter, appended in the order in which the array parameters appear later. The form is as follows:$arr = Array (' C ', ' B ', ' A ', ' D ');$arr 1= Array (' 1 ', ' 2 ', ' 3 ', ' 4 ');$arr 2= array_merge ($arr, $arr 1); Print_r ($arr 2);//array ([0] = = C [1] = B [2] = = A [3] = + D [4] = 1 [5] = 2 [6]=> 3 [7] = 4)

** **
**recursive append array**The

**array_merge_recursive () function** is the same as Array_merge (), which merges two or more arrays together to form an array of unions. The difference between the two is that when a key in an input array already exists in the result array, the function takes a different approach. Array_merge () overrides the previously existing key/value pair, replaces it with the key/value pair in the current input array, and array_merge_recursive () merges two values together to form a new array with the original key as the array name. The form is:$arr = Array (' One ' = ' C ', ' one ' = ' B '); $arr 1= Array (' three ' = ' 1 ', ' one ' = ' 2 '); $arr 2= array_merge_recursive ($arr, $arr 1); Print_r ($arr 2);//array ([One] = Array ([0] = B [1] = 2) [three] + 1)

**merging two arrays**The

**array_combine ()** function generates a new array that consists of a set of committed keys and corresponding values in the form of:$arr = Array (' A ', ' B '), $arr 1= Array (' 1 ', ' 2 '), $arr 2= array_combine ($arr, $arr 1);p Rint_r ($arr 2); Array ([A] = 1 [B] = 2)

**splitting a fraction group**The

**array_slice () function** returns a portion of the array, starting with the key offset and ending at the offset+length position. When offset is positive, the split starts at offset from the beginning of the array, and if offset is a negative value, the split starts at the offset position at the end of the array. If the optional parameter length is omitted, the split starts at offset and continues to the last element of the array. If length is given and is positive, the offset+length position ends at the beginning of the array. Conversely, if length is given and is negative, the count-|length| position at the beginning of the array ends. The form is as follows: $arr = Array (' A ', ' B ', ' C ', ' D '); $arr 1= array_slice ($arr, 2, 1);p Rint_r ($arr 1); Array ([0] + = C)

**to find the intersection of arrays**The

**array_intersect () function** returns an array that retains the key, which consists only of the values that appear in the first array and that appear in each of the other input arrays. The form is as follows:$arr = Array (' A ', ' B ', ' C ', ' D '); $arr 1= Array (' A ', ' B ', ' E '); $arr 2= Array (' A ', ' F ', ' D '); $arr 3= array_intersect ($arr, $arr 1 , $arr 2);p Rint_r ($arr 3);//array ([0] = A) Note: Array_intersect () considers them equal only if two elements have the same data type.

**to find the intersection of associative arrays**ARRAY_INTERSECT_ASSOC () is basically the same as Array_intersect (), except that it also considers the keys of the array in the comparison. Therefore, only key/value pairs that appear in the first array and in all other input arrays are returned to the result array. The form is as follows: $arr = Array (' a ' = = ' A ', ' b ' = = ' B ', ' c ' = = ' C ', ' d ' = = ' d '); $arr 1= Array (' a ' + = ' a ', ' c ' = = ' B ', ' E '); arr2= Array (' a ' = = ' A ', ' b ' = ' F ', ' d ' = ' B '); $arr 3= Array_intersect_assoc ($arr, $arr 1, $arr 2);p Rint_r ($arr 3); /array ([a] = a)

**finding the difference set of associative arrays**The function Array_diff_assoc () is basically the same as Array_diff (), except that it also considers the keys of the array at the time of comparison, so only the key/value pairs that appear in the first array and not in the other input arrays are returned to the result array. The form is as follows: $arr = Array (' a ' = = ' A ', ' b ' = = ' B ', ' c ' = = ' C ', ' d ' = = ' d '); $arr 1= Array (' a ' + = ' A ', ' b ' = = ' B ', ' e ' = ' E '); $arr 3= Array_diff_assoc ($arr, $arr 1);p Rint_r ($arr 3);//array ([c] = c [d] + D)

**other useful array functions**Returns a random set of keys

**Array_rand () function**One or more keys in the array are returned. The form is: $arr = Array (' a ' = = ' A ', ' B ' = ' B ', ' c ' = ' C ', ' d ', ' = ' d '), $arr 1= Array_rand ($arr, 2);p Rint_r ($arr 1);// Array ([0] = b [1] = d)

**Random Shuffle array elements**The

**Shuffle () function** randomly re-sorts the array elements.

**sum the values in an array**The

**array_sum () function** adds all the values in the array together, returning the final and form as follows:$arr = Array (' A ', ' + ', ' B '), $count = Array_sum ($arr);p Rint_r ($count),//44 if the array contains other data types (such as strings), the values are ignored.

**dividing an array**The

**array_chunk () function** decomposes An array into a multidimensional array that consists of multiple arrays containing size elements. The form is as follows:$arr = Array (' A ', ' B ', ' C ', ' D '), $arr 1= array_chunk ($arr, 2);p Rint_r ($arr 1);//array ([0] = = Array ([0] = A [1] => ; B) [1] = = Array ([0] = C [1]=> D))

Summary of PHP array functions