Home > Article > Backend Development > Summary of common array functions in PHP (with code examples)
1. Some basic operation functions on keys and values of arrays
1. Get all the keys or values of the array:array_keys()
array_values()
$arr_keys = array_keys($array);$arr_values = array_values($arr);
2. Exchange the positions of keys and values in the array. If the previous one is repeated, it will be overwritten by the later one: array_flip()
$arr2 = array_flip($arr);
3. The given value Whether it is in the array: in_array(value,array)
$bool = in_array('hello',$arr);
4. Search for a value in the array, and return its key if it is there, or FALSE if it is not: array_search ()
$bool = array_search('hello',$arr);
5. Whether the given key exists in the array:isset(array[key])
andarray_key_exists(key,array)
$bool = array_key_exists('a',$arr);
6. Get the number of array elements: count(array,mode)
. When mode is 1, it means recursively counting the array. The default is 0. Alias sizeof()
$n = count($arr); //等价于:$n = sizeof($arr);
7. Change the key name in the array to all lowercase or uppercase: array_change_key_case(array,case)
. There are two commonly used variables in case: CASE_UPPER
or CASE_LOWER
(default value), which is all lowercase by default
$lowarr = array_change_key_case($arr,CASE_LOWER);
8. Count the number of occurrences of all values in the array: array_count_value(array)
. Return an array, the key is the value of the original array, the value is the number of times this element appears in the original array
$arr_count = array_count_values($arr);
9. Get the first or last key name of the array: array_key_first(array)
, array_key_last(array)
$key = array_key_first($arr)
10. Pop the last element of the array:
$last = array_pop($array);
Push one or more cells to the end of the array or the beginning of the array , and returns the number of new arrays:
$new_array = array_push($array,$value1,$value2,...);$new_array = array_unshift($array,$value1,$value2,...);
11. Reverse the array: array_reverse(array)
$reverse = array_reverse($arr)
12. Sum all the values in the array or Find the product:
$sum = array_sum($array);$product = array_product($array);
13. Remove duplicate values from the array:
array_unique($array,,SORT_STRING);sort_falgs参数用于修改排序行为:SORT_NUMERIC - 按照数字形式比较、SORT_STRING - 按照字符串形式比较
14. Shuffle the array: shuffle(array)
$bool = shuffle($arr);
15 . Randomly obtain one or more key names from the array: array_rand(array,num=1)
, and return an array containing random key names.
2. Summary of some operation functions for creating and splitting arrays
1. Split an array into multiple arrays:array_chunk(array,size,preserve_keys)
Parameters: size
: Specify the number of elements in each array preserve_keys
: Specify whether to retain the original key name, the default is false
.
The function returns a two-dimensional array
$myarr = array_chunk($arr,2)
2. Create an array, using the value of one array as its key name and the value of another array as its value: array_combine(keys,values)
$arr_1 = ['A','B','C'];$arr_2 = ['a','b','c'];$arr_3 = array_combine($arr_1,$arr_2);
3. Fill the array with the specified keys and values: array_fill_keys(keys,value)
$keys = array('foo', 5, 10, 'bar');$a = array_fill_keys($keys, 'banana');
4. Fill the array with the given values: array_fill(start_index,num,value)
Parameters: start_index
: The first index of the array num
: The number of inserted elements The quantity, that is, the length of the array, must be a non-negative number value
: the value used to fill
$arr = array_fill(0,10,'myname');
5. Merge one or more arrays: array_merge(array1, array2...)
When the key names are the same, if the key name is a character, it will be overwritten, but the numeric key name will not be overwritten, but will be appended to the end
$a = array_merge($arr_1,$arr_2);
6. Recursive Merge one or more arrays: array_merge_recursive(array_1,array_2,...)
If the arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to Next.
7. Fill a value into the array with the specified length:array_pad(array,size,value)
Parameters:size
: The length of the array after filling. If it is positive, it will be filled to the right side of the array. If it is negative, it will be filled to the left side of the array. value
: The value used to fill
8. From the array Take a section out of: array_slice(array,offset,length,preserve_keys)
Parameters: offset
: The starting offset, either positive or negativelength
: The obtained length, a positive number indicates the number of elements obtained, a negative number indicates the distance from the end of the array preserve_keys
: Whether to retain the original key name
10. Remove a certain part of the array and replace it with other values: array_splice(array,offset,length,replacement_array)
Parameters:replacement_array
: The removed unit is The unit replacement in this array
11. Use variables to create an array:compact(var1,var2,...)
, the variable name is the key name, and the variable value is the value of the element
12. Export variables from the array: extract(array)
, the key is the variable name, and the value is the value of the variable
13. Assign the value of the array to the variable: list(var1,var2,...)
list($drink, , $power) = array('coffee', 'brown', 'caffeine');
14. Create an array based on the range, containing the specified elements:range(start,end,step)
range(0,8,2) ==> [0,2,4,6,8]
三、数组排序基本函数名为 sort
可以添加其他拓展:r表示逆向排序,k表示对键名进行排序,a表示保持索引关系,u表示用自定义的函数进行比较 1.详细介绍sort函数的情况,其他的函数类似
/*bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )SORT_REGULAR参数可以用以下值改变排序的行为:SORT_REGULAR - 正常比较单元(不改变类型)SORT_NUMERIC - 单元被作为数字来比较SORT_STRING - 单元被作为字符串来比较SORT_LOCALE_STRING - 根据当前的区域(locale)设置来把单元当作字符串比较,可以用 setlocale() 来改变。SORT_NATURAL - 和 natsort() 类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。SORT_FLAG_CASE - 能够与 SORT_STRING 或 SORT_NATURAL 合并(OR 位运算),不区分大小写排序字符串。 */
排序函数分类大致如下:
2.sort()、rsort():对值进行升序和降序的排序3.ksort()、krsort():对键名进行升序和降序的排序4.asort()、arsort():保持索引关系的同时,对值进行升序和降序的排序5.usort()、uksort()、uasort():使用自定义的排序函数,进行按值的升序排序、按键名的升序排序、保持索引关系的升序排序6.natsort():使用自然排序算法对数组进行排序7.natcasesort():使用自然排序算法对数组进行不区分大小写字母的排序
四、数组运算
数组差集的计算 u表示用自定义的回调函数,diff表示用数据做差集,assoc表示用索引做差集
1.计算数组的差集:array_diff(array1,array2,...)
对比array1和其他数组,返回在array1中但不在其他数组中的值。返回一个数组,但是键名不保留
2.用回调函数比较数据来计算数组的差集:array_udiff(arr1,arr2,...,value_cpmpare_func)
使用用户自定义的函数进行数据比较,而不是内置的函数。
3.使用键名比较计算数组的差集:array_diff_key(array1,array2,...)
使用键名而不是值来进行差集计算
4.用回调函数对键名比较计算数组的差集:array_diff_ukey(arr1,arr2,...,key_compare_func)
5.带索引检查计算数组的差集:array_diff_assoc(array1,array2,..)
同时使用键名和值来进行差集计算
6.带索引检查计算数组的差集,用回调函数比较索引:array_diff_uassoc(arr1,arr2,...,key_compare_func)
key_compare_func
:用户自定义的用于比较键名的函数。
7.带索引检查计算数组的差集,用回调函数比较数据:array_udiff_assoc(arr1,arr2,...,value_cpmpare_func)
value_cpmpare_func
:用户自定义的用于比较数据的函数
8.用回调函数比较数据和索引,计算数组的差集:array_udiff_uassoc(arr1,arr2,...,value_cpmpare_func,key_compare_func)
数组交集的计算 同差集一样,也有8个函数:
array_intersect() 使用数据进行交集比较array_uintersect() 使用数据进行交集比较,但是自定义函数比较array_intersect_key() 使用键名进行交集比较array_intersect_ukey() 使用键名进行交比较,但是自定义函数比较array_intersect_assoc() 同时使用数据和键名array_intersect_uassoc() 同时使用数据和键名,但是键名使用自定义函数比较array_uintersect_assoc() 同时使用数据和键名,但是数据使用自定义函数比较array_uintersect_uassoc() 同时使用数据和键名,都使用自定义函数
推荐学习:《PHP视频教程》
The above is the detailed content of Summary of common array functions in PHP (with code examples). For more information, please follow other related articles on the PHP Chinese website!