Here, we will see the functions that can be used with an array. We’ll divide the different functions into sections to make it more understandable and readable.
In this list of functions, there are two that are actually constructors, one is array, and the other is list
Something to keep in mind is that array is not an object, and list isn’t either. these 2 are estructures of the language
I will take a break for a few days, and then I will continue with this section of the blog. Thank you for understanding
Checks if the given key or index exists in the array
bool
array_is_list
Checks whether a given array is a list
bool
key
Fetch a key from an array
int | string | null
array_rand
Pick one or more random keys out of an array
int | string | array
array_keys
Return all the keys or a subset of the keys of an array
array
array_column
Return the values from a single column in the input array
array
array_count_values
Counts the occurrences of each distinct value in an array
array
count / sizeof
Counts all elements in an array or in a Countable object
int
array_unique
Removes duplicate values from an array
array
array_values
Return all the values of an array
array
array_key_first
Gets the first key of an array
int | string | null
array_key_last
Gets the last key of an array
int | string | null
Array intersection
When we talk about intersection, we are referring to the intersection between arrays. Let’s imagine the arrays as sets, so picture the sets as circles. A1 is the red circle, A2 is the green circle, and A3 is the blue circle
For intersections between arrays, PHP provides us with the following functions:
function
description
return
array_intersect_assoc
Computes the intersection of arrays with additional index check
array
array_intersect_key
Computes the intersection of arrays using keys for comparison
array
array_intersect_uassoc
Computes the intersection of arrays with additional index check, compares indexes by a callback function
array
array_intersect_ukey
Computes the intersection of arrays using a callback function on the keys for comparison
array
array_intersect
Computes the intersection of arrays
array
array_uintersect_assoc
Computes the intersection of arrays with additional index check, compares data by a callback function
array
array_uintersect_uassoc
Computes the intersection of arrays with additional index check, compares data and indexes by separate
array
array_uintersect
Computes the intersection of arrays, compares data by a callback function
array
Functions for obtaining the difference
These functions will give us the difference with respect to another array or other arrays, based on their values or keys. If we view the arrays as sets, it would be something like this: A1 is the red circle, A2 is the green circle, and A3 is the blue circle.
These are the functions that PHP has for calculating the difference:
function
description
return
array_diff_assoc
Computes the difference of arrays with additional index check
array
array_diff_key
Computes the difference of arrays using keys for comparison
array
array_diff_uassoc
Computes the difference of arrays with additional index check which is performed by a user supplied callback function
array
array_diff_ukey
Computes the difference of arrays using a callback function on the keys for comparison
array
array_diff
Computes the difference of arrays
array
array_udiff_assoc
Computes the difference of arrays with additional index check, compares data by a callback function
array
array_udiff_uassoc
Computes the difference of arrays with additional index check, compares data and indexes by a callback function
array
array_udiff
Computes the difference of arrays by using a callback function for data comparison
array
Functions for sorting an array
function
description
return
sort
Sort an array in ascending order
true
uasort
Sort an array with a user-defined comparison function and maintain index association
true
uksort
Sort an array by keys using a user-defined comparison function
true
usort
Sort an array by values using a user-defined comparison function
true
natcasesort
Sort an array using a case insensitive “natural order” algorithm
true
natsort
Sort an array using a “natural order” algorithm
true
krsort
Sort an array by key in descending order
true
ksort
Sort an array by key in ascending order
true
array_multisort
Sort multiple or multi-dimensional arrays
bool
arsort
Sort an array in descending order and maintain index association
true
asort
Sort an array in ascending order and maintain index association
true
rsort
Sort an array in descending order
true
array_reverse
Return an array with elements in reverse order
array
array_flip
Exchanges all keys with their associated values in an array
array
shuffle
Shuffle an array
true
Functions for partitioning or slicing an array
function
description
return
array_chunk
Split an array into chunks
array
array_slice
Extract a slice of the array
array
array_splice
Remove a portion of the array and replace it with something else
array
Arrays iteration
function
description
return
array_filter
Filters elements of an array using a callback function
array
array_walk_recursive
Apply a user function recursively to every member of an array
array
array_walk
Apply a user supplied function to every member of an array
array
array_map
Applies the callback to the elements of the given arrays
array
Merge Arrays
When we perform a merge, more than simply joining, what we do is a fusion between them, so if they have equal values and keys, the new array will not repeat them. So will not be duplicated elements in the new array.
function
description
return
array_merge_recursive
Merge one or more arrays recursively
array
array_merge
Merge one or more arrays
array
Combine Arrays
The combination is quite simple to understand. If we have 2 arrays, it will take the values from the first array and use them as keys in the new array. The values from the second array will become the values of the new array.
function
description
return
array_combine
Crea un nuevo array, usando una matriz para las claves y otra para sus valores
aray
Position Management
function
description
return
current / pos
Return the current element in an array
mixed
end
Set the internal pointer of an array to its last element
mixed
next
Advance the internal pointer of an array
mixed
prev
Rewind the internal array pointer
mixed
reset
Set the internal pointer of an array to its first element
mixed
Reemplace elements
function
description
return
array_replace_recursive
Replaces elements from passed arrays into the first array recursively
array
array_replace
Replaces elements from passed arrays into the first array
array
Maths operations
function
description
return
array_reduce
Iteratively reduce the array to a single value using a callback function
mixed
array_sum
Calculate the sum of values in an array
int|float
array_product
Calculate the product of values in an array
int|float
Compact and extract
function
description
return
compact
Create array containing variables and their values
array
extract
Import variables into the current symbol table from an array
I'm working on Berserker that is package for PHP right now you can check it progress at https://packagist.org/packages/luisg/berserker and the doc at https://blastcoding.com/en/php-berserker/