The content of this article is about the array of PHP built-in methods. It has a certain reference value. Now I share it with you. Friends in need can refer to it
PHP 5 Array function
Function | Description |
---|---|
array() | Create an array |
array_change_key_case() | Change all keys in the array to lowercase or uppercase |
array_chunk() | Split an array into new array chunks |
array_column() | Return the value of a single column in the input array |
array_combine() | Creates a new array by merging two arrays |
Used to count the number of occurrences of all values in the array. | |
Compare arrays and return the difference set (only compare key values). | |
Compare arrays and return the difference set (compare key names and key values). | |
Compare arrays and return the difference set (only compare key names). | |
Compare arrays and return the difference set (compare key names and key values, use user-defined key name comparison function). | |
Compare arrays and return the difference set (only compare key names, use user-defined key name comparison function). | |
Fills the array with the given key value. | |
Fills an array with the given key value for the specified key name. | |
Use the callback function to filter the elements in the array. | |
Exchange the keys and values in the array. | |
Compare arrays and return the intersection (only compare key values). | |
Compare arrays and return intersection (compare key name and key value). | |
Compare arrays and return the intersection (only compare key names). | |
Compare arrays and return intersection (compare key name and key value, use user-defined key name comparison function). | |
Compare arrays and return intersection (only compare key names, use user-defined key name comparison function). | |
Check whether the specified key name exists in the array. | |
Returns all key names in the array. | |
Send each value in the array to the user-defined function and return the new value. | |
Merge one or more arrays into one array. | |
Recursively merge one or more arrays. | |
Sort multiple arrays or multidimensional arrays. | |
Pad the array to the specified length with values. | |
Delete the last element of the array (pop). | |
Calculate the product of all values in an array. | |
Insert one or more elements into the end of the array (push). | |
Returns one or more random keys in the array. | |
Returns an array as a string by using a user-defined function. | |
Replace the value of the first array with the value of the subsequent array. | |
Recursively replace the value of the first array with the value of the subsequent array. | |
Returns an array in reverse order. | |
Search for the given value in the array and return the key name. | |
Deletes the first element in the array and returns the value of the deleted element. | |
Returns the selected part of the array. | |
Removes and replaces the specified elements in the array. | |
Returns the sum of the values in the array. | |
Compare arrays and return the difference set (only compare values, using a user-defined key comparison function). | |
Compare arrays and return difference sets (compare keys and values, use built-in functions to compare key names, use user-defined functions to compare key values) . | |
Compare arrays and return the difference set (compare keys and values, using two user-defined key name comparison functions). | |
array_uintersect() | Compares arrays and returns the intersection (only compares values, using a user-defined key comparison function). |
array_uintersect_assoc() | Compare arrays and return the intersection (compare keys and values, use built-in functions to compare key names, use user-defined functions to compare key values). |
array_uintersect_uassoc() | Compare arrays and return the intersection (compare keys and values, using two user-defined key name comparison functions). |
array_unique() | Remove duplicate values from the array. |
array_unshift() | Insert one or more elements at the beginning of the array. |
array_values() | Returns all the values in the array. |
array_walk() | Apply a user function to each member of the array. |
array_walk_recursive() | Applies a user function recursively to each member of an array. |
arsort() | Sort the associative array in descending order by key value. |
asort() | Sort the associative array in ascending order by key value. |
compact() | Creates an array containing variable names and their values. |
count() | Returns the number of elements in the array. |
current() | Returns the current element in the array. |
each() | Returns the current key/value pair in the array. |
end() | Point the internal pointer of the array to the last element. |
extract() | Import variables from the array into the current symbol table. |
in_array() | Checks whether the specified value exists in the array. |
key() | Get the key name from the associative array. |
krsort() | Sort the array in reverse order by key name. |
ksort() | Sort the array by key name. |
list() | Assign the values in the array to some variables. |
natcasesort() | Use the "natural sorting" algorithm to sort the array in a case-insensitive manner. |
natsort() | Sort the array using the "natural sorting" algorithm. |
next() | Move the internal pointer in the array forward one position. |
pos() | Alias for current(). |
prev() | Rewind the internal pointer of the array by one bit. |
range() | Creates an array containing cells in the specified range. |
reset() | Point the internal pointer of the array to the first element. |
rsort() | Sort the array in reverse order. |
shuffle() | Shuffle the array. |
sizeof() | An alias for count(). |
sort() | Sort the array. |
uasort() | Use a user-defined comparison function to sort the key values in the array. |
uksort() | Use a user-defined comparison function to sort the key names in the array. |
usort() | Sort the array using a user-defined comparison function. |
Convert all keys of the array to uppercase letters:
$age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31"); print_r(array_change_key_case($age,CASE_UPPER));
Definition and usage
array_change_key_case() function will All array keys are converted to uppercase or lowercase letters.
The numeric index of the array does not change. If the optional argument (i.e. the second argument) is not provided, it defaults to lowercase.
Note: If two or more keys are the same when running this function, the last element will overwrite the other elements
Split the array into an array with two elements, and retain the key names in the original array:
$age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31","David"=>"35"); print_r(array_chunk($age,2,true));
The third parameter can be omitted, default false (do not retain the original key name and generate an index array)
Retrieve the last_name column from the record set and use the corresponding "id" column as the key Value:
$a = array( array( 'id' => 5698, 'first_name' => 'Bill', 'last_name' => 'Gates', ), array( 'id' => 4767, 'first_name' => 'Steve', 'last_name' => 'Jobs', ) array( 'id' => 3809, 'first_name' => 'Mark', 'last_name' => 'Zuckerberg', ) ); $last_names = array_column($a, 'last_name', 'id'); print_r($last_names); //输出 Array( [5698] => Gates [4767] => Jobs [3809] => Zuckerberg )
The third parameter is not filled in, and the index array is returned
By merging two arrays To create a new array, one of the array elements is the key name and the other array element is the key value:
$key = array("Bill","Steve","Mark"); $value = array("60","56","31"); $c=array_combine($key, $value); //输出 array("Bill"=>'60', "Steve"=>'56', "Mark"=>'31');
key key name array
value key value array
Count all the values in the array:
$a=array("A","Cat","Dog","A","Dog"); print_r(array_count_values($a));
This The function returns an array, the key name of its element is the value of the original array, and the key value is the number of times the value appears in the original array.
Related recommendations:
How to deal with PHP array problems
The above is the detailed content of Array of PHP built-in methods. For more information, please follow other related articles on the PHP Chinese website!