Advertisement

Google Ad Slot: content-top

PHP Array Functions


PHP offers a rich set of built-in functions to work with arrays. Here are some of the commonly used array functions in PHP:

FUNCTION

DESCRIPTION

array()

Creates an array

array_push($array, $value)

Adds one or more elements to the end of an array

array_pop($array)

Removes and returns the last element of an array

array_unshift($array, $value)

Adds one or more elements to the beginning of an array

array_shift($array)

Removes and returns the first element of an array

array_key_exists($key, $array)

Checks if a specified key exists in the array

array_keys($array)

Returns all the keys of an array

array_values($array)

Returns all the values of an array.

array_filter($array, $callback)

Filters elements of an array using a callback function

array_map($callback, $array)

Applies a callback function to each element of an array.

array_reduce($array, $callback)

Reduces an array to a single value using a callback function

in_array($needle, $array)

Checks if a value exists in an array

array_search($needle, $array)

Searches the array for a value and returns its key if found

sort($array)

Sorts an array in ascending order

rsort($array)

Sorts an array in descending order

ksort($array)

Sorts an array by key in ascending order

krsort($array)

Sorts an array by key in descending order

array_chunk($array, $size)

Splits an array into chunks of specified size

array_merge($array1, $array2)

Merges two or more arrays

array_combine($keys, $values)

Creates an array by using one array for keys and another for values

array_slice($array, $offset, $length)

Extracts a portion of an array

array_splice($array, $offset, $length, $replacement)

Removes a portion of an array and replaces it

count($array)

Counts the number of elements in an array

array_reverse($array)

Returns an array with elements in reverse order

array_unique($array)

Removes duplicate values from an array