PHP arrays are a way to store multiple items of the same type in one variable.
They are useful for making a list of similar items. You can access items in an
array using their index number or key. PHP has many built-in functions to work
with and change array items. These functions work with both single and
multi-dimensional arrays.
Installation: You do not need to install anything extra to use array
functions. They are included in PHP by default.
Example: Here is a PHP program that sorts an array in ascending order using
the
`sort()`
function and then prints the sorted array.
<?php // Create an array object $arr = array(1, 4, 3, 2, 6); // Sort function to sort // array elements sort($arr); // Prints the sorted array print_r($arr); ?>
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 6 )
The complete list of Array Functions are given below:
PHP Array Functions | Description |
---|---|
array_chunk() | Split an array into parts or chunks of a given size. |
array_combine() | Create a new array by using one array for keys and another array for values. |
array_count_values() | Count all the values inside an array. |
array_diff_assoc() | Get the difference between one or more arrays. (Compares keys and values) |
array_diff_keys() | Get the difference between one or more arrays. (Compares keys only) |
array_diff_uassoc() | Compare the keys and values of the user-defined function. |
array_diff_ukey() | Compares the key to two or more arrays (Uses a user-defined key comparison function). |
array_diff() | Calculate the difference between two or more arrays. (Compares values only) |
array_fill_keys() | Create a new array filled with the given keys and value provided as an array to the function. |
array_fill() | Fill an array with values. |
array_filter() | Filter the elements of an array using a user-defined function. |
array_flip() | Exchange elements within an array (Swaps keys and values). |
array_intersect_assoc() | Compute the intersection of two or more arrays. (Compares keys and values) |
array_intersect_key() | Compute the intersection of two or more arrays. (Compares keys only) |
array_intersect_uassoc() | Compare key and values of two or more arrays (Uses a user-defined comparison function). |
array_intersect() | Compare the values of two or more arrays and returns the matches. |
array_key_exists() | Check whether a specific key or index is present inside an array or not. |
array_keys() | Return either all the keys of an array or the subset of the keys. |
array_merge_recursive() | Merge two or more arrays into a single array recursively. |
array_multisort() | Sort multiple arrays at once or a multi-dimensional array with each individual dimension. |
array_pad() | Pad a value fixed number of time onto an array. |
array_pop() | Delete or pop out and return the last element from an array passed to it as a parameter. |
array_product() | The products of all the elements in an array. |
array_push() | Push new elements into an array. |
array_rand() | Fetch a random number of elements from an array. |
array_reduce() | Reduce the elements of an array into a single value |
array_replace_recursive() | Replaces the values of the first array with the values of the following arrays. (Recursive) |
array_replace() | Replaces the values of the first array with the values of the following arrays. |
array_reverse() | Reverse the elements of an array including the nested arrays. |
array_search() | Search for a particular value in an array and return the key if found. |
array_shift() | Shifts an element off from the beginning in an array and returns the shifted value. |
array_slice() | Fetch a part of an array by slicing through it, according to the users choice. |
array_splice() | Replaces the existing element with elements from other arrays and returns an array. |
array_sum() | It takes an array parameter and returns the sum of all the values in it. |
array_udiff_assoc() | The function computes the difference arrays from two or more arrays (Uses a user-defined comparison function). |
array_udiff() | Distinguishing between two or more arrays (Uses a user-defined comparison function). |
array_uintersect_assoc() | Compute the intersection of the array of keys for different values of two or more arrays (Uses a user-defined comparison function). |
array_uintersect_uassoc() | Computes the intersection of two arrays (Uses a user-defined comparison function). |
array_uintersect() | Compute the intersection of two or more arrays depending on the values (Compares values only). |
array_unique() | This function removes duplicate values from an array. |
array_unshift() | Elements are added to the beginning of the array. |
array_values() | Get an array of values from another array that may contain key-value pairs or just values. |
array_walk_recursive() | The array element's keys and values are parameters in the callback function (Applies a user function to every element recursively). |
array_walk() | It is a user-defined function to every element of the array. |
array() | This is used to create an array. |
arsort() | Sort an array according to values in descending order. |
compact() | Create an array using variables. |
count() | Count the current elements in the array. |
current() | Return the value of the element in an array which is the internal pointer. |
end() | Find the last element of the given array. |
extract() | The extract() function does array to variable conversion. |
in_array() | Check whether a given value exists in an array or not. |
key() | Return the index of the element of a given array to which is the internal pointer. |
krsort() | Sort an array by key in reverse order according to its index values. |
ksort() | Sort an array in ascending order according to its key values. |
list() | Assign array values to multiple variables at a time. |
natcasesort() | Sort an array by using a “natural order” algorithm (Case-insensitive). |
natsort() | Sort an array by using a “natural order”, it does not check the type of value for comparison |
next() | The next() function increments the internal pointer after returning the value. |
pos() | Return the value of the element in an array to which the internal pointer is currently pointing. |
prev() | Return the immediate previous element from an array. |
range() | Create an array of elements of any kind such as integers, alphabets within a given range. |
reset() | Move any array's internal pointer to the first element of that array. |
rsort() | Sort the array in descending order i.e, greatest to smallest. |
shuffle() | Shuffle or randomize the order of the elements in an array. |
sizeof() | Count the number of elements present in an array or any other countable object (Alias of count()). |
sort() | Sort an array in ascending order i.e, smaller to greater. |
uasort() | Sort an array such that array indices maintain their correlation with the array elements (Sorts by elements, preserving key-value pairs). |
uksort() | Sort an array according to the keys and not values using a user-defined comparison function. |
usort() | Sort arrays in an easier way. Here, we are going to discuss a new function usort() (Sorts by values). |
each() | Get the current element key-value pair of the given array to which the internal pointer is currently pointing |