The most commonly used data structure in PHP is arrays. Arrays can help us solve a large part of programming problems. In fact, there are many array operations that we don’t need to write ourselves. The system already comes with related functions. Below we introduce some functions about PHP array operations, maybe some of which you have not heard of.
- Search for a specific value in an array, returning TRUE if found otherwise returning FALSE
boolean in_array(mixed needle,array haystack[,boolean strict])
Copy after login
- Find a specified key in the array, return TRUE if found, otherwise return FALSE
boolean array_eky_exists(mixed key,array array)
Copy after login
- Search for a specific value in an array, returning TRUE if found otherwise returning FALSE
boolean array_search(mixed needle,array haystack[,boolean strict])
Copy after login
- Get a new array composed of all keys of the array
array array_keys(array array[,mixed search_value])
Copy after login
- Get a new array composed of all values in the array
array array_values(array array)
Copy after login
- Determine array size
integer count(array array[,int mode])
integer sizeof(array array[,int mode])
Copy after login
- Count the frequency of occurrence of array elements
array array_count_values(array array)
Copy after login
- Delete duplicate values in the array and return an array composed of unique values
array array_unique(array array)
Copy after login
- Reverse the order of the array elements. If preserve_key is TRUE, the order of the array key values will remain unchanged
array array_reverse(array array[,boolean preserve_key])
Copy after login
- Replace array keys and values
array array_flip(array array)
Copy after login
- Array order sorting, sort_flags parameter is optional, default behavior
SORT_NUMBERIC,按数值排序,对整数或浮点数排序很有用
SORT_REGULAR,按照ASCII值排序
SORT_STRING,按接近人所认识的正确顺序排序
asort函数键值顺序不变
void sort(array array[,int sort_flags])
void asort(array array[,int sort_flags])
Copy after login
- The array is sorted in reverse order, the sort_flags parameter is optional, and the default behavior is
SORT_NUMBERIC,按数值排序,对整数或浮点数排序很有用
SORT_REGULAR,按照ASCII值排序
SORT_STRING,按接近人所认识的正确顺序排序
arsort函数键值顺序不变
void rsort(array array[,int sort_flags])
void arsort(array array[,int sort_flags])
Copy after login
- Natural sorting of arrays
void natsort(array array)
Copy after login
- Case-insensitive natural sorting
void natcasesort(array array)
Copy after login
- Sort the array by key value
boolean ksort(array array[,int sort_flags])
Copy after login
- Sort the key-value array in reverse order
boolean krsort(array array[,int sort_flags])
Copy after login
- Sort according to user-defined order
void usort(array array,callback function_name)
Copy after login
- Merge the arrays together and return a combined array. The back of array_merge covers the front, array_merge_recursive merges together
array array_merge(array array1[array array2……])//一个以上
array array_merge_recursive(array array1,array array2[,array ……])//两个以上
Copy after login
- Keys and values form a new array
array array_combine(array key,array value)
Copy after login
- Returns a part of the array, starting from offset and ending at offset+length
array array_slice(array array, int offset [,int length])
Copy after login
- Delete all elements starting from offset and ending at offset+length, and return the deleted elements in the form of an array
array array_splice(array, int offset [,int length[,array peplacement]])
Copy after login
- Find the intersection of arrays, the key value is the key value in the first array
array array_intersect(array array1,array array2[,arrayN……])
Copy after login
- Find the intersection of the arrays if the key value is equal, and the key value is the key value in the first array
array array_intersect_assoc(array array1,array array2[,arrayN……])
Copy after login
- Find the difference set of arrays, the first array has a value that is not found in other arrays
array array_diff(array array1,array array2[,arrayN……])
Copy after login
- Find the difference set of arrays. The first array contains equal key values in values that are not found in other arrays
array array_diffassoc(array array1,array array2[,arrayN……])
Copy after login
- Returns one or more key values in the array
mixed array_rand(array array[,int num_entries])
Copy after login
- Shuffle function
void shuffle(array input_array)
Copy after login
- Sum the values in an array
mixed array_sum(array array);
Copy after login
- Decompose the array into a multi-dimensional array, which contains size elements
array array_chunk(array array, int size [,boolean preserve_keys])
Copy after login