Home > Backend Development > PHP Tutorial > Array of PHP built-in methods

Array of PHP built-in methods

不言
Release: 2023-03-24 19:26:01
Original
1266 people have browsed it

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

Arrays



PHP 5 Array function

Function Description

##array_count_values()Used to count the number of occurrences of all values ​​in the array. array_diff()Compare arrays and return the difference set (only compare key values). array_diff_assoc()Compare arrays and return the difference set (compare key names and key values). array_diff_key()Compare arrays and return the difference set (only compare key names). array_diff_uassoc()Compare arrays and return the difference set (compare key names and key values, use user-defined key name comparison function). array_diff_ukey()Compare arrays and return the difference set (only compare key names, use user-defined key name comparison function). array_fill()Fills the array with the given key value. array_fill_keys()Fills an array with the given key value for the specified key name. array_filter()Use the callback function to filter the elements in the array. array_flip()Exchange the keys and values ​​in the array. array_intersect()Compare arrays and return the intersection (only compare key values). array_intersect_assoc()Compare arrays and return intersection (compare key name and key value). array_intersect_key()Compare arrays and return the intersection (only compare key names). array_intersect_uassoc()Compare arrays and return intersection (compare key name and key value, use user-defined key name comparison function). array_intersect_ukey()Compare arrays and return intersection (only compare key names, use user-defined key name comparison function). array_key_exists()Check whether the specified key name exists in the array. array_keys()Returns all key names in the array. array_map()Send each value in the array to the user-defined function and return the new value. array_merge()Merge one or more arrays into one array. array_merge_recursive()Recursively merge one or more arrays. array_multisort() Sort multiple arrays or multidimensional arrays. array_pad()Pad the array to the specified length with values. array_pop()Delete the last element of the array (pop). array_product()Calculate the product of all values ​​in an array. array_push()Insert one or more elements into the end of the array (push). array_rand()Returns one or more random keys in the array. array_reduce() Returns an array as a string by using a user-defined function. array_replace()Replace the value of the first array with the value of the subsequent array. array_replace_recursive()Recursively replace the value of the first array with the value of the subsequent array. array_reverse()Returns an array in reverse order. array_search()Search for the given value in the array and return the key name. array_shift()Deletes the first element in the array and returns the value of the deleted element. array_slice()Returns the selected part of the array. array_splice()Removes and replaces the specified elements in the array. array_sum()Returns the sum of the values ​​in the array. array_udiff()Compare arrays and return the difference set (only compare values, using a user-defined key comparison function). array_udiff_assoc()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) . array_udiff_uassoc()Compare arrays and return the difference set (compare keys and values, using two user-defined key name comparison functions).
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
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.

array_change_key_case()

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));
Copy after login

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

array_chunk( )

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));
Copy after login
  • The third parameter can be omitted, default false (do not retain the original key name and generate an index array)

array_column()

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
)
Copy after login
  • The third parameter is not filled in, and the index array is returned

array_combine()

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');
Copy after login
  • key key name array

  • value key value array

array_count_values()

Count all the values ​​in the array:

$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
Copy after login
  • 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template