Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |not finished | checked

PHP array functions

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

Constructors

constructordescriptionreturn
arraycreates an arrayarray
listcreate a list with array dataarray

Create an array

function/constructordescriptionreturn
rangearray
arraycreates an arrayarray

Array Modification

functiondescriptionreturn
array_​padPad array to the specified length with a valuearray
array_​pop Pop the element off the end of arraymixed
array_​pushPush one or more elements onto the end of arrayint
array_​searchSearches the array for a given value and returns the first corresponding key if successfulint | string | false
array_​shift Shift an element off the beginning of arraymixd
array_​unshiftPrepend one or more elements to the beginning of an arrayint
array_​change_​key_​caseChanges the case of all keys in an arrayarray

Getting data from an Array

functiondescriptionreturn
in_​arrayChecks if a value exists in an arraybool
key_​exists / array_​key_​existsChecks if the given key or index exists in the arraybool
array_​is_​listChecks whether a given array is a listbool
keyFetch a key from an arrayint | string | null
array_​randPick one or more random keys out of an arrayint | string | array
array_​keysReturn all the keys or a subset of the keys of an arrayarray
array_​columnReturn the values from a single column in the input arrayarray
array_​count_​valuesCounts the occurrences of each distinct value in an arrayarray
count / sizeofCounts all elements in an array or in a Countable objectint
array_​uniqueRemoves duplicate values from an arrayarray
array_​valuesReturn all the values of an arrayarray
array_​key_​first Gets the first key of an arrayint | string | null
array_​key_​last Gets the last key of an arrayint | 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:

functiondescriptionreturn
array_​intersect_​assocComputes the intersection of arrays with additional index checkarray
array_​intersect_​keyComputes the intersection of arrays using keys for comparisonarray
array_​intersect_​uassocComputes the intersection of arrays with additional index check, compares indexes by a callback functionarray
array_​intersect_​ukey Computes the intersection of arrays using a callback function on the keys for comparisonarray
array_​intersectComputes the intersection of arraysarray
array_​uintersect_​assocComputes the intersection of arrays with additional index check, compares data by a callback functionarray
array_​uintersect_​uassocComputes the intersection of arrays with additional index check, compares data and indexes by separatearray
array_​uintersectComputes the intersection of arrays, compares data by a callback functionarray

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:

functiondescriptionreturn
array_​diff_​assocComputes the difference of arrays with additional index checkarray
array_​diff_​keyComputes the difference of arrays using keys for comparisonarray
array_​diff_​uassocComputes the difference of arrays with additional index check which is performed by a user supplied callback functionarray
array_​diff_​ukeyComputes the difference of arrays using a callback function on the keys for comparisonarray
array_​diffComputes the difference of arraysarray
array_​udiff_​assocComputes the difference of arrays with additional index check, compares data by a callback functionarray
array_​udiff_​uassocComputes the difference of arrays with additional index check, compares data and indexes by a callback functionarray
array_​udiffComputes the difference of arrays by using a callback function for data comparisonarray

Functions for sorting an array

functiondescriptionreturn
sortSort an array in ascending ordertrue
uasortSort an array with a user-defined comparison function and maintain index associationtrue
uksortSort an array by keys using a user-defined comparison functiontrue
usortSort an array by values using a user-defined comparison functiontrue
natcasesortSort an array using a case insensitive “natural order” algorithmtrue
natsortSort an array using a “natural order” algorithmtrue
krsortSort an array by key in descending ordertrue
ksortSort an array by key in ascending ordertrue
array_​multisort Sort multiple or multi-dimensional arraysbool
arsortSort an array in descending order and maintain index associationtrue
asortSort an array in ascending order and maintain index associationtrue
rsortSort an array in descending ordertrue
array_​reverseReturn an array with elements in reverse orderarray
array_​flipExchanges all keys with their associated values in an arrayarray
shuffleShuffle an arraytrue

Functions for partitioning or slicing an array

functiondescriptionreturn
array_​chunkSplit an array into chunksarray
array_​sliceExtract a slice of the arrayarray
array_​splice Remove a portion of the array and replace it with something elsearray

Arrays iteration

functiondescriptionreturn
array_​filterFilters elements of an array using a callback functionarray
array_​walk_​recursiveApply a user function recursively to every member of an arrayarray
array_​walkApply a user supplied function to every member of an arrayarray
array_​mapApplies the callback to the elements of the given arraysarray

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.

functiondescriptionreturn
array_​merge_​recursive Merge one or more arrays recursivelyarray
array_​mergeMerge one or more arraysarray

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.

functiondescriptionreturn
array_​combineCrea un nuevo array, usando una matriz para las claves y otra para sus valoresaray

Position Management

functiondescriptionreturn
current / posReturn the current element in an arraymixed
endSet the internal pointer of an array to its last elementmixed
nextAdvance the internal pointer of an arraymixed
prevRewind the internal array pointermixed
resetSet the internal pointer of an array to its first elementmixed

Reemplace elements

functiondescriptionreturn
array_​replace_​recursiveReplaces elements from passed arrays into the first array recursivelyarray
array_​replaceReplaces elements from passed arrays into the first arrayarray

Maths operations

functiondescriptionreturn
array_​reduceIteratively reduce the array to a single value using a callback functionmixed
array_​sumCalculate the sum of values in an arrayint|float
array_​productCalculate the product of values in an arrayint|float

Compact and extract

functiondescriptionreturn
compactCreate array containing variables and their valuesarray
extractImport variables into the current symbol table from an arrayint
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category