Summary of commonly used functions in PHP

There are quite a lot of functions in php, and the functions are quite powerful. If you can't think of it, you can write it yourself. First, it is troublesome, and second, the efficiency is definitely not as high as the built-in functions of php, so to sum up, you can check it later.

1. About key and value processing

1.array_values($array)
The function returns an array containing all key values ​​in the given array, but does not keep the key names.
2.array_keys($array)
The function returns a new array containing all the key names in the array.
3.array_change_key_case($array[,CASE_UPPER/CASE_LOWER])
The return value is that the function converts all the keys of the array to uppercase or lowercase letters, and the original array does not change. If two or more keys are the same when the function is run, the last element overwrites the others.
4.array_column($array,"column_key"[,"index_key"])
If it is in this format array_column($array,"column_key"), return the value of a single column in the input multidimensional array, and the key value is in the form of self-increment.
If array_column($array,"column_key","index_key"), return the value of the index_key column as the key and the value of the column_key column as an array of values. If the value of the index_key column is repeated as a key, the latter will overwrite the former.
5.in_array(search,$array[,type])
The function searches an array for the existence of the specified value. type is optional. If this parameter is set to true, checks whether the searched data is of the same type as the array's values.
6.array_search(value,$array[,strict])
The function searches for a key value in a one-dimensional array and returns the corresponding key name, otherwise it returns false. If the third parameter strict is specified as true, the key name of the corresponding element is returned only if the data type and value are consistent.
7.array_key_exists(key,$array)
The function checks whether the specified key exists in an array, returns true if the key exists, and returns false if the key does not exist.
8.array_flip($array)
The function returns a reversed array, if the same value appears more than once, the last key name will be used as its value, and all other key names will be lost.
If the data type of the values ​​in the original array is not string or integer, the function will report an error.
9.array_reverse($array[,preserve])
The function flips the order of the elements in the original array, creates a new array and returns. For indexed arrays, if the second parameter is specified as true, the key names of the elements remain unchanged, otherwise the key names are lost.

2. Full value statistics

1.array_sum($array)
The function returns the sum of all the values ​​in the array. Returns an integer value if all values ​​are integers. Returns a float if one or more of these values ​​is a float.
2.array_product($array)
The function computes and returns the product of an array.
3.count($array[,mode])/sezeof($array[,model]);
The function counts the number of cells in an array or the number of properties in an object. model,0 - Default. Do not count all elements in a multidimensional array; 1 - recursively count the number of elements in the array (count all elements in a multidimensional array)
4.array_count_values($array)
The function is used to count the number of occurrences of all values ​​in an array. This function returns an array whose key name is the value of the original array, and the key value is the number of times the value appears in the original array.

3. Cut and delete processing

1.array_chunk($array,size[,preserve_key])
The function splits the array into new array blocks. The number of cells in each array is determined by the size parameter. The last array may have fewer cells. The optional parameter preserve_key is a boolean value that specifies whether the elements of the new array have the same key as the original array (for associative arrays), or a new numeric key starting at 0 (for indexing arrays). The default false is to assign a new key.
2.array_slice(array,start[,length][,preserve])
The function extracts a value from an array according to a condition and returns it. Regarding length, specifies the length of the returned array. If the value is set to an integer, the number of elements will be returned; if the value is set to a negative number, the function will terminate at the end of the example array so far; if the value is not set , returns all elements from the position set by the start parameter to the end of the array. preserve, optional, true - preserve key names; false - default, reset key names.
3.array_splice($array,start[,length][,$array])
The function removes the selected element from the array and replaces it with the new element. The function will also return an array containing the removed elements. start, a required parameter, specifies the starting position of the deleted element, 0 represents the first element, if the value is set to a positive number, it will be removed from the offset specified by the value in the array, if the value is set to a negative number , the removal starts at the offset specified by the reciprocal of the value from the end of the array. length, optional, specifies the number of elements to be removed, and is also the length of the returned array; if the value is set to a positive number, remove the number of elements; if the value is set to a negative number, remove from start to All elements up to the reciprocal length of the end of the array; if this value is not set, all elements from the position set by the start parameter to the end of the array are removed. The last array, optional, specifies the array with the elements to be inserted into the original array. If there is only one element, it can be set to a string, and does not need to be set to an array.
4.array_unique($array)
The function removes duplicate values ​​from an array and returns the resulting array. When the values ​​of several array elements are equal, only the first element is kept, and the other elements are deleted. The key names in the returned array are unchanged.

Fourth, filling processing

1.array_pad($array,size,value)
The function inserts the specified number of elements with the specified value into the array. If you set the size parameter to a negative number, the function inserts new elements before the original array.
2.array_fill(index,number,value)
Fills the array with the key value, the function fills the array with the given value, and the returned array has number elements and the value is value. The returned array is numerically indexed, starting at index position and increasing.
3.array_fill_keys($keys,value)
Populates an array with the specified keys and values. $keys uses the values ​​of this array as keys. Illegal values ​​will be converted to strings. value The value with which to populate the array.

Five, set operations

1.array_diff($array1,$array2,$array3...)
The function returns the difference array of two arrays. This array includes all the keys in the array being compared ($array1), but not in any of the other parameter arrays ($array2, $array3...). In the returned array, the key names remain the same.
2.array_diff_key(array1,array2,array3...)
Returns an array containing all the key names in array1, but not in any of the other parameter arrays (array2 or array3, etc.).
3.array_diff_assoc($array1,$array2[,$array3...])
Compares the keys and values ​​of two (or more) arrays and returns a difference array that includes all the arrays being compared (array1), but not in any of the other argument arrays (array2 or array3, etc.) ) in the key name and key value.
4.array_diff_ukey($array1,$array2[,$array3...],myfunction)
This function compares the key names of two (or more) arrays and returns a difference array that includes all the arrays being compared (array1), but not in any of the other argument arrays (array2 or array3, etc. ) in the key name. myfunction calls the comparison function for strings. If the first argument is less than, equal to, or greater than the second argument, the comparison function must return an integer less than, equal to, or greater than 0. E.g:
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"black","b"=>"yellow","d"=>"brown");
$a3=array("e"=>"purple","f"=>"white","a"=>"gold");

$result=array_diff_ukey($a1,$a2,$a3,"myfunction");
print_r($result);
5.array_diff_uassoc(array1,array2[,array3...],myfunction)
This function compares the keys and values ​​of two (or more) arrays and returns a difference array that includes all of the arrays being compared (array1), but not in any of the other argument arrays (array2 or array3). etc.) in the key name and key value. myfunction defines a string that can call the comparison function. If the first argument is less than, equal to, or greater than the second argument, the comparison function must return an integer less than, equal to, or greater than 0.
6.array_intersect(array1,array2[,array3...])
This function compares the keys of two (or more) arrays and returns the intersection array, which includes all the arrays being compared (array1), as well as any other parameter arrays (array2 or array3, etc.) key value in .
7.array_intersect_key(array1,array2[,array3...])
This function compares the key names of two (or more) arrays and returns the intersection array, which includes all the arrays being compared (array1), as well as any other parameter arrays (array2 or array3, etc.) key name in .
8.array_intersect_assoc(array1,array2[,array3...])
This function compares the keys and values ​​of two (or more) arrays and returns the intersection array, which includes all the arrays being compared (array1), but also any other parameter arrays (array2 or array3, etc.) etc.) in the key name and key value.
9.array_intersect_ukey(array1,array2[,array3...],myfunction)
Returns an array containing the values ​​of all key names that appear in array1 and also appear in all other parameter arrays.
This comparison is made through a user-supplied callback function. The function takes two parameters, two key names to compare against. The function returns a negative number if the first parameter is less than the second parameter, 0 if the two parameters are equal, and a positive number if the first parameter is greater than the second parameter.
10.array_intersect_uassoc(array1,array2[,array3...],myfunction)
This function compares the keys and values ​​of two (or more) arrays and returns the intersection array, which includes all the arrays being compared (array1), but also any other parameter arrays (array2 or array3, etc.) etc.) in the key name and key value.
11.array_merge(array1[,array2][,array3...])
The function combines one or more arrays into one array. Note that if two or more array elements have the same key, the last element will overwrite the others.
12.array_merge_recursive(array1[,array2][,array3...])
The function combines one or more arrays into one array. This function differs from the array_merge() function in handling two or more array elements with the same key name. array_merge_recursive() does not perform key name overwriting, but recursively combines multiple values ​​with the same key name into an array.

Six, data structure processing

1.array_push(array,value1[,value2...])
The function adds one or more elements to the end of the array in the first argument (pushes) and returns the length of the new array.
2.array_pop(array)
The function removes the last element in the array and returns the value of the removed element.
3.array_shift(array)
The function deletes the first element in the array and returns the value of the deleted element.
4.array_unshift(array,value1[,value2][,value3...])
The function is used to insert new elements into an array. The values ​​of the new array will be inserted at the beginning of the array. Note: Numeric key names will start at 0 and increase by 1. String key names will remain unchanged.
The above functions can simulate structures such as stacks and queues.

Seven, sorting function

1.sort(array[,sortingtype])
The function sorts an array of indices in ascending order. Note that the original key name will be deleted. There are many types of sortingtype,
0 = SORT_REGULAR - default. Arrange each item in normal order (Standard ASCII, no change of type).
1 = SORT_NUMERIC - treat each item as a number. 2 = SORT_STRING - treat each item as a string.
3 = SORT_LOCALE_STRING - treat each item as a string, based on the current locale (can be changed with setlocale()).
4 = SORT_NATURAL - treat each item as a string, using a natural sort like natsort().
5 = SORT_FLAG_CASE - Strings can be sorted by combining (bitwise OR) SORT_STRING or SORT_NATURAL, case insensitive.
2.rsort(array[,sortingtype])
Sort the index array in descending order. The sortingtype type is the same as above.
3.asort(array[,sortingtype])
The function sorts an associative array in ascending order by key value. The sortingtype type is the same as above.
4.arsort(array[,sortingtype])
The function sorts an associative array in descending order by key value. The sortingtype type is the same as above.
5.ksort(array[,sortingtype])
The function sorts an associative array in ascending order by key name. The sortingtype type is the same as above.
6.krsort(array[,sortingtype])
The function sorts an associative array in descending order by key name. The sortingtype type is the same as above.

8. Use the callback function

1.array_reduce(array,myfunction[,initial])
The function uses a callback function to iteratively reduce the array to a single value. If a third parameter is specified, it will be treated as the first value in the array, or as the final return value if the array is empty.
2.array_map(myfunction,array1[,array2][,array3...])
The function applies a user-defined function to each value in the array, and returns an array with the new values ​​after the user-defined function has been applied. The number of arguments accepted by the callback function should match the number of arrays passed to the array_map() function.
3.array_filter(array,callbackfunction)
The function filters the values ​​in the array with a callback function. This function passes each key value in the input array to the callback function. If the callback function returns true, the current key value in the input array is returned to the result array. The array key names remain the same.

9. Other functions

1.range(low,high[,step])
This function creates an array of integers or characters from low to high inclusive. If high is less than low, the reversed array is returned. step, optional, defaults to 1.
2.array_rand(array[,number])
The function randomly selects one or more elements from the array and returns it. Two parameters are used to determine how many elements to select. Returns an array containing random key names if more than one element is selected, otherwise returns the key name of the element.
3.shuffle(array)
The function rearranges the elements of an array in random order. Note that this function assigns new key names to elements in the array, and existing key names will be deleted.
4.array_combine(keys,values)
The function creates a new array by merging two arrays, one of which is the key name and the other is the value of the key. The number of elements in the key name array and the key value array must be the same!
5.implode([separator,]array)
The function returns a string composed of array elements. separator is optional and specifies the content to be placed between the elements of the array. Default is "" (empty string).
Same usage as join().
6.explode(separator,string[,limit])
The function breaks a string into an array. limit is optional and specifies the number of returned array elements.
limit, optional
greater than 0 - returns an array with up to limit elements
less than with all but the last -limit elements
0 - returns an array with one element

10. Mathematical functions

1.abs(x)
find absolute value
2.ceil(x)
Ceil means ceiling in Chinese, and the function rounds up to the nearest integer. Note that negative numbers, such as ceil(-5.9), have a value of -5.
3.floor(x)
floor in Chinese means floor, the function rounds down to the nearest integer. Negative numbers, such as floor(-5.1), have a value of -6.
4.fmod(x,y)
Returns the floating-point remainder when the dividend (x) is divided by the divisor (y). The remainder (r) is defined as: x = i * y + r, where i is an integer. If y is nonzero, then r and x have the same sign and are less than y in magnitude.
5.pow(x,y)
Returns x raised to the power of y.
6.round(x,prec)
The function rounds a floating point number. prec is optional and specifies the number of digits after the decimal point.
7.sqrt(x)
Returns the square root of x.
8.max(x,y)
Returns the value with the largest numerical value among the parameters. If there is only one parameter and it is an array, max() returns the largest value in the array.
9.min (x, y)
Find the smallest value.

Eleven, about strings

1.trim(string,charlist)
The function removes whitespace or other predefined characters from both sides of a string.
ltrim(string, charlist) removes blank strings or other predefined characters from the left side of the string.
rtrim(string, charlist) removes blank strings or other predefined characters from the right of the string.
2.str_pad(string,length[,pad_string][,pad_type])
The function pads the string to the new length. length is required, specifies the new length of the string, if the value is less than the original length of the string, do nothing.
pad_string is optional, specifies the string used for padding, the default is blank.
pad_type, optional, specifies which side of the string to pad.
Possible values:
STR_PAD_BOTH - pads both sides of the string. If it's not even, the right side gets extra padding.
STR_PAD_LEFT - pads the left side of the string.
STR_PAD_RIGHT - Pads the right side of the string. default.
3.str_repeat(string,repeat)
The function repeats the string a specified number of times.
4.str_split(string[,length])
length is optional and specifies the length of each array element. Default is 1.
5.strrev(string)
The function reverses a string.
6.wordwrap(string[,width][,break][,cut])
cut is optional and specifies whether to wrap words larger than the specified width:
FALSE - default. No-wrap
TRUE - wrap lines
7.str_shuffle(string)
The function randomly shuffles all the characters in the string.
8.strtolower(string) converts the string to lowercase.
lcfirst(string) converts the first letter of the string to lowercase.
9.strtoupper(string) converts the string to uppercase.
ucfirst(string) converts the first letter of the string to uppercase.
ucwords() converts the first character of each word in the string to uppercase.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939040&siteId=291194637