PHP Array Functions
PHP provides a wide range of array functions to manipulate and interact with arrays. Here's a categorized list of commonly used PHP array functions with brief descriptions:
Array Creation & Manipulation
-
array()
– Creates an array. -
array_merge()
– Merges one or more arrays. -
array_combine()
– Combines two arrays (keys from one, values from another). -
array_slice()
– Extracts a portion of an array. -
array_splice()
– Removes or replaces elements. -
array_push()
– Adds elements to the end. -
array_pop()
– Removes the last element. -
array_unshift()
– Adds elements to the beginning. -
array_shift()
– Removes the first element.
Array Search & Check
-
in_array()
– Checks if a value exists. -
array_key_exists()
– Checks if a key exists. -
array_search()
– Searches for a value and returns the key.
Array Keys & Values
-
array_keys()
– Returns all keys. -
array_values()
– Returns all values.
Sorting Arrays
-
sort()
– Sorts array in ascending order. -
rsort()
– Sorts array in descending order. -
asort()
– Sorts array while maintaining key association. -
ksort()
– Sorts by key in ascending order. -
krsort()
– Sorts by key in descending order.
Array Filtering & Mapping
-
array_filter()
– Filters elements using a callback function. -
array_map()
– Applies a callback to elements. -
array_walk()
– Applies a callback to elements by reference. -
array_reduce()
– Reduces array to a single value via a callback.
Array Set Operations
-
array_unique()
– Removes duplicate values. -
array_diff()
– Computes the difference between arrays. -
array_intersect()
– Computes intersection of arrays.
Array Utility
-
count()
– Counts elements. -
sizeof()
– Alias ofcount()
. -
array_sum()
– Sums values. -
array_product()
– Multiplies all values. -
array_rand()
– Picks one or more random keys. -
shuffle()
– Randomizes element order.