Wednesday, September 25, 2024
Google search engine
HomeLanguagesPHP Array Functions Complete Reference

PHP Array Functions Complete Reference

PHP Arrays are a type of data structure that allows to storing of multiple elements of a similar type under a single variable by saving the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. The array functions are allowed to interact and manipulate the array elements in various ways. The PHP array inbuilt functions are used for simple and multi-dimensional arrays.

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

Below programs illustrate the array_diff_uassoc() function in PHP: 

PHP




<?php
// PHP program to illustrate the
// array_diff_uassoc() function
  
// user defined function that returns 0 if
// $array1's keys are equal to any other
// input array, else returns 1 if greater,
// or -1 if smaller
function user_function($a, $b)
{
if ($a===$b)
{
    return 0;
}
return ($a>$b)? 1: -1;
}
  
// Input Arrays
$a1=array(10=>"striver", 20=>"raj", 30=>"geek");
$a2=array(20=>"striver", 10=>"raj", 30=>"geek");
  
$result = array_diff_uassoc($a1, $a2, "user_function");
  
print_r($result);
?>


Output:

Array
(
    [10] => striver
    [20] => raj
)

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

   

RELATED ARTICLES

Most Popular

Recent Comments