Skip to content

php_ref_array

Arrays are a fundamental data structure in PHP, allowing you to store multiple values in a single variable. PHP provides a rich set of functions to create, access, and manipulate arrays effectively.

PHP supports both numerically indexed arrays (starting from index 0 by default) and associative arrays (using string keys). Multi-dimensional arrays (arrays containing other arrays) are also fully supported.

Modern PHP versions (since PHP 5.4) favor the short array syntax [] over the older array() construct for better readability:

<?php
// Old syntax
$indexedOld = array(1, 2, 3);
$assocOld = array('a' => 1, 'b' => 2);
// Modern syntax (preferred)
$indexedNew = [1, 2, 3];
$assocNew = ['a' => 1, 'b' => 2];
?>

Array functions are built into the PHP core. No installation or special configuration is needed to use them.

Here is a list of commonly used built-in array functions in PHP. For a complete and up-to-date list, always refer to the official PHP documentation.

FunctionDescription
[] / array()Creates an array (use [] in modern PHP).
array_change_key_case()Changes all keys in an array to lowercase or uppercase.
array_chunk()Splits an array into chunks of smaller arrays.
array_column()Returns the values from a single column in a multi-dimensional array.
array_combine()Creates an array by using one array for keys and another for its values.
array_count_values()Counts how many times each value occurs in an array.
array_diff()Computes the difference of arrays (compares values only).
array_diff_assoc()Computes the difference of arrays with additional index check (compares keys and values).
array_diff_key()Computes the difference of arrays using keys for comparison.
array_fill()Fills an array with values.
array_fill_keys()Fills an array with values, specifying keys.
array_filter()Filters elements of an array using a callback function.
array_flip()Exchanges all keys with their associated values in an array.
array_intersect()Computes the intersection of arrays (compares values only).
array_intersect_assoc()Computes the intersection of arrays with additional index check (compares keys and values).
array_intersect_key()Computes the intersection of arrays using keys for comparison.
array_is_list()Checks if the array keys are a sequence from 0 to count()-1 (PHP 8.1+). Useful for distinguishing indexed vs associative arrays.
array_key_exists() / key_exists()Checks if the specified key exists in the array.
array_keys()Returns all the keys or a subset of the keys of an array.
array_map()Applies a callback function to the elements of the given arrays.
array_merge()Merges one or more arrays. Note: Later associative keys overwrite earlier ones. Numeric keys are re-indexed.
array_merge_recursive()Merges one or more arrays recursively.
array_multisort()Sorts multiple or multi-dimensional arrays.
array_pad()Pads array to the specified length with a value.
array_pop()Pops the element off the end of array.
array_product()Calculates the product of values in an array.
array_push()Pushes one or more elements onto the end of array.
array_rand()Picks one or more random keys out of an array.
array_reduce()Iteratively reduces the array to a single value using a callback function.
array_replace()Replaces values from passed arrays into the first array.
array_replace_recursive()Replaces values from passed arrays into the first array recursively.
array_reverse()Returns an array with elements in reverse order.
array_search()Searches the array for a given value and returns the first corresponding key if successful.
array_shift()Shifts the first element off the beginning of array.
array_slice()Extracts a slice of the array.
array_splice()Removes a portion of the array and replaces it with something else.
array_sum()Calculates the sum of values in an array.
array_unique()Removes duplicate values from an array.
array_unshift()Prepends one or more elements to the beginning of an array.
array_values()Returns all the values from the array and indexes the array numerically.
array_walk()Applies a user supplied function to every member of an array.
array_walk_recursive()Applies a user supplied function recursively to every member of an array.
arsort()Sorts an associative array in descending order, according to the value.
asort()Sorts an associative array in ascending order, according to the value.
compact()Creates an array containing variables and their values.
count() / sizeof()Counts all elements in an array or properties in an object. count() is preferred.
current()Returns the current element in an array.
end()Sets the internal pointer of an array to its last element.
extract()Imports variables into the current symbol table from an array. Use with caution due to potential variable overwrites.
in_array()Checks if a value exists in an array.
key()Fetches the key of the current element from an array.
krsort()Sorts an associative array in descending order, according to the key.
ksort()Sorts an associative array in ascending order, according to the key.
list()Assigns variables as if they were an array. Modern PHP also supports destructuring assignment: [$a, $b] = $array;.
natcasesort()Sorts an array using a case insensitive “natural order” algorithm.
natsort()Sorts an array using a “natural order” algorithm.
next()Advances the internal array pointer.
prev()Rewinds the internal array pointer.
range()Creates an array containing a range of elements.
reset()Sets the internal pointer of an array to its first element.
rsort()Sorts an indexed array in descending order.
shuffle()Shuffles an array (randomizes the order of elements).
sort()Sorts an indexed array in ascending order.
uasort()Sorts an array by values using a user-defined comparison function, maintaining key association.
uksort()Sorts an array by keys using a user-defined comparison function.
usort()Sorts an array by values using a user-defined comparison function.

Note: The each() function was deprecated in PHP 7.2 and removed in PHP 8.0. Use foreach loops or functions like current(), key(), next(), prev(), and reset() to iterate over arrays and manage the internal pointer.

Best Practice: For iterating over arrays, the foreach loop is generally the most readable and recommended approach:

<?php
$colors = ['red', 'green', 'blue'];
// Iterate over values
foreach ($colors as $color) {
echo "Color: $color\n";
}
$capitals = ['USA' => 'Washington D.C.', 'UK' => 'London'];
// Iterate over keys and values
foreach ($capitals as $country => $city) {
echo "Capital of $country is $city\n";
}
?>

Tip: For very large datasets, consider using Generators (yield) instead of building large arrays in memory to improve performance and reduce memory consumption.