Open In App
Related Articles

PHP Array Functions

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

PHP Arrays are a data structure that stores multiple elements of a similar type in a single variable. The arrays are helpful to create a list of elements of similar type. It can be accessed using their index number or key. The array functions are allowed to interact and manipulate the array elements in various ways. The PHP array functions are used for single and multi-dimensional arrays.

Installation: The array functions have not required any installation. These are part of PHP core concepts.

Example: PHP program to sort an array in ascending order using sort() function and print the sorted array.

PHP

<?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

Example

array_chunk()Split an array into parts or chunks of a given size.
Try
array_combine()Create a new array by using one array for keys and another array for values.
Try
 array_count_values() Count all the values inside an array.
Try
array_diff_assoc()Get the difference between one or more arrays.
Try
array_diff_keys()Get the difference between one or more arrays. 
Try
array_diff_uassoc()Compare the keys and values of the user-defined function.
Try
array_diff_ukey()Compares the key to two or more arrays 
Try
array_diff() Calculate the difference between two or more arrays. 
Try
 array_fill_keys()Create a new array filled with the given keys and value provided as an array to the function.
Try
array_fill()Fill an array with values.
Try
array_filter() Filter the elements of an array using a user-defined function.
Try
array_flip()Exchange elements within an array
Try
array_intersect_assoc()Compute the intersection of two or more arrays.
Try
array_intersect_key()Compute the intersection of two or more arrays.
Try
array_intersect_uassoc()Compare key and values of two or more arrays 
Try
array_intersect()Compare the values of two or more arrays and returns the matches.
Try
array_key_exists()Check whether a specific key or index is present inside an array or not.
Try
array_keys()Return either all the keys of an array or the subset of the keys.
Try
array_merge_recursive()Merge two or more arrays into a single array recursively. 
Try
array_multisort()Sort multiple arrays at once or a multi-dimensional array with each individual dimension.
Try
array_pad()Pad a value fixed number of time onto an array.
Try
array_pop()Delete or pop out and return the last element from an array passed to it as a parameter.
Try
array_product()The products of all the elements in an array.
Try
array_push()Push new elements into an array.
Try
array_rand() Fetch a random number of elements from an array.
Try
array_reduce() Reduce the elements of an array into a single value
Try
array_replace_recursive()Replaces the values of the first array with the values
Try
array_replace()It takes a list of arrays separated by commas (,) as parameters 
Try
array_reverse()Reverse the elements of an array including the nested arrays.
Try
array_search() Search for a particular value in an array
Try
array_shift()Shifts an element off from the beginning in an array.
Try
array_slice()Fetch a part of an array by slicing through it, according to the users choice.
Try
array_splice()Replaces the existing element with elements from other arrays and returns an array 
Try
array_sum()It takes an array parameter and returns the sum of all the values in it.
Try
array_udiff_assoc()The function computes the difference arrays from two or more arrays
Try
array_udiff()Distinguishing between two or more arrays.
Try
array_uintersect_assoc()Compute the intersection of the array of keys for different values of two or more arrays.
Try
array_uintersect_uassoc() Computes the intersection of two arrays.
Try
array_uintersect() Compute the intersection of two or more arrays depending on the values
Try
array_unique() This function removes duplicate
Try
array_unshift() Elements are added to the beginning of the array. 
Try
array_values() Get an array of values from another array that may contain key-value pairs or just values.
Try
array_walk_recursive()The array element’s keys and values are parameters in the callback function.
Try
array_walk() It is a user-defined function to every element of the array.
Try
array() This is used to create an array. 
Try
arsort() Sort an array according to values.
Try
compact()Create an array using variables. 
Try
count() Count the current elements in the array. 
Try
current() Return the value of the element in an array which is the internal pointer.
Try
end()Find the last element of the given array.
Try
extract() The extract() function does array to variable conversion. 
Try
in_array()Check whether a given value exists in an array or not.
Try
key() Return the index of the element of a given array to which is the internal pointer.  
Try
krsort() Sort an array by key in reverse order according to its index values.
Try
ksort() Sort an array in ascending order according to its key values.
Try
list()Assign array values to multiple variables at a time.
Try
natcasesort()Sort an array by using a “natural order” algorithm.
Try
natsort()Sort an array by using a “natural order”, it does not check the type of value for comparison
Try
next()The next() function increments the internal pointer after returning the value
Try
pos()Return the value of the element in an array to which the internal pointer is currently pointing.
Try
prev()Return the immediate previous element from an array
Try
range()Create an array of elements of any kind such as integers, alphabets within a given range
Try
reset()Move any array’s internal pointer to the first element of that array.
Try
rsort()Sort the array in descending order i.e, greatest to smallest.
Try
shuffle()Shuffle or randomize the order of the elements in an array.
Try
sizeof()Count the number of elements present in an array or any other countable object.
Try
sort()Sort an array in ascending order i.e, smaller to greater.
Try
uasort()Sort an array such that array indices maintain their correlation with the array elements 
Try
uksort()Sort an array according to the keys and not values using a user-defined comparison function.
Try
usort()Sort arrays in an easier way. Here, we are going to discuss a new function usort(). 
Try
each()Get the current element key-value pair of the given array to which the internal pointer is currently pointing
Try

   



Last Updated : 30 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads