A PHP array holds many values under one variable name. Under the hood it is an ordered map: every value sits under a key, and the keys are integers or strings. The same type works as a plain list, a lookup table, a stack, a queue, or a set, which is why arrays show up in almost every PHP script.
This page explains the ideas. The Array Functions index lists all 88 function guides.
Pick the right tool.
Use count() and in_array() to inspect, the four stack functions to add or remove at the ends, array_map(), array_filter() and array_reduce() to transform, and the sort family to order. Almost every array function either returns a new array or changes the one you pass by reference, so check which one before you rely on the result.
Arrays are copied, not shared.
Assigning $b = $a gives $b its own copy of the values. Changing $b later does not change $a. Only functions that take the array by reference, such as sort() and array_push(), change the variable you pass in.
Some old functions are gone.
each() was removed in PHP 8.0, so avoid it in new code and use foreach instead. The index still documents it for old code you may meet. list() is a language construct, not a real function, and the short form [$a, $b] = $array does the same job.
Syntax
PHP Syntax
The usual pattern: create an array, inspect it, change it, then loop or transform it:
PHP
// create: a list and an associative array
$fruits = ['apple', 'banana', 'cherry'];
$stock = ['apple' => 5, 'banana' => 0, 'cherry' => 12];
// inspect
echo count($fruits); // 3
var_dump(in_array('banana', $fruits, true)); // bool(true)
echo $stock['cherry']; // 12
// stack and queue
$fruits[] = 'date'; // append (same as array_push)
array_unshift($fruits, 'avocado'); // add to the front
$last = array_pop($fruits); // remove from the end
$first = array_shift($fruits); // remove from the front
// transform with callbacks
$lengths = array_map('strlen', $fruits); // [5, 6, 6]
$stocked = array_filter($stock, fn($n) => $n > 0); // apple, cherry
$total = array_reduce($stock, fn($c, $n) => $c + $n, 0); // 17
// combine and compare
$all = array_merge(['a', 'b'], ['c']); // a, b, c
$only = array_diff(['a', 'b', 'c'], ['b']); // a, c
$shared = array_intersect(['a', 'b'], ['b', 'c']); // b
// order
sort($fruits); // by value, new keys
ksort($stock); // by key, keeps values
// keys and values
$names = array_keys($stock); // apple, banana, cherry
$counts = array_values($stock); // 5, 0, 12
Merge joins arrays, diff keeps what is only in the first, intersect keeps what is in all. Variants such as _key, _assoc and u compare keys or use your own function
array_slice() returns a piece and leaves the original alone; array_splice() removes or replaces a piece in place
Passing by reference
The sort functions, the four stack functions, shuffle() and array_splice() change the array in place and return a simple value. Do not write $fruits = sort($fruits);, because sort() returns true, not the sorted array. By contrast, array_map(), array_filter(), array_merge() and array_slice() return a new array that you must assign to a variable.
Reference
Lists vs Associative Arrays
PHP has one array type, but you use it in two common shapes. A list has the keys 0, 1, 2 in order. An associative array uses names as keys. Knowing which shape you have tells you which functions are safe to use.
List (indexed)
Associative
Example
['apple', 'banana']
['apple' => 5, 'banana' => 0]
Keys
Integers 0, 1, 2 in order
Strings (or any integers) you choose
Best for
Ordered items you loop over
Records and lookups by name
Read one item
$list[1]
$map['banana']
Sort by value
sort() (renumbers the keys)
asort() (keeps the keys)
Check the shape
array_is_list() returns true only when the keys are 0, 1, 2 in order (PHP 8.1+)
A list can turn into a non-list without warning. After unset($list[1]) or after array_filter() the keys have gaps, so array_is_list() returns false. Call array_values() to renumber the keys when you need a list again.
Modern PHP
PHP 8.x Array Helpers
Newer PHP versions add small helpers that replace common foreach loops. They are short, so here is the whole set at a glance:
The first or last value, or null for an empty array
Check your PHP version.
On PHP older than 8.4, calling array_find() gives Call to undefined function. Run echo PHP_VERSION; to see what your server uses. The examples below avoid the 8.4 functions, so they run on any PHP 8.1 or newer.
Code
PHP Examples
Three short scripts that show how arrays work. Each one uses fixed fruits and numbers, so the output is the same every time you run it.
Example 1 — Create, count, search, push and pop
Start with a list of fruits and a stock table, then search them and add and remove items at both ends.
Fruits: 3
Has banana: yes
Has grape: no
Cherry stock: 12
After adding: avocado, apple, banana, cherry, date
Removed: avocado and date
Now: apple, banana, cherry
How It Works
count($fruits) returns the number of elements, 3. in_array(..., true) does a strict search and returns a boolean, so 'banana' is found and 'grape' is not.
$stock['cherry'] reads a value by its key. This is the associative shape: the key is a name, not a position.
array_push() adds 'date' at the end and array_unshift() adds 'avocado' at the front, so the list grows to five items.
array_pop() removes and returns the last item (date) and array_shift() removes and returns the first (avocado). The array is back to its three original fruits.
Example 2 — array_map(), array_filter() and array_reduce()
Square a list of numbers, keep the even ones, and add them all up. The original array never changes.
array_map() runs the arrow function on every element and returns a new array of the results. Squaring 1 to 6 gives 1, 4, 9, 16, 25, 36.
array_filter() keeps the elements for which the callback returns true. The even numbers are 2, 4 and 6, and they keep their original keys 1, 3 and 5.
array_values() renumbers the keys to 0, 1, 2. Use it when you need a clean list after filtering.
array_reduce() carries a running total through the array. It starts at 0 and adds each number, ending at 21. The last line shows that $numbers was not changed by any of the three calls.
Example 3 — Merge, diff, intersect and sort
Compare two fruit lists, then sort a list of numbers and a stock table by value and by key.
Merge: apple, banana, cherry, banana, cherry, date
Unique: apple, banana, cherry, date
Diff: apple
Intersect: banana, cherry
sort: 5, 8, 12, 30
rsort: 30, 12, 8, 5
ksort: apple, banana, cherry
asort keys: banana, apple, cherry
asort values: 0, 5, 12
numbers is a list: yes
stock is a list: no
How It Works
array_merge() joins the two lists and keeps duplicates. array_unique() drops the repeats, leaving four fruits.
array_diff($a, $b) keeps the values that are in $a but not in $b, which is only apple. array_intersect() keeps the values found in both, banana and cherry.
sort() orders the numbers from low to high and rsort() from high to low. Both change $numbers in place and renumber the keys.
ksort() sorts the stock table by key, and asort() then sorts it by value while each value keeps its key. That is why the keys come out as banana, apple, cherry for the values 0, 5, 12.
array_is_list() (PHP 8.1) says yes for the numbers, whose keys are 0 to 3, and no for the stock table, whose keys are names.
Continue
Next: PHP Array Functions Index
After you know how arrays, callbacks, and sorting fit together, open the functions index for all 88 guides.
A PHP array is an ordered map that holds many values under one variable name. Each value is stored under a key, which is either an integer or a string. You can create one with the short syntax [] or with the array() construct, and it can hold values of any type, including other arrays.
An indexed array (a list) uses the keys 0, 1, 2 and so on in order, for example ['apple', 'banana']. An associative array uses names you choose as keys, for example ['apple' => 5, 'banana' => 0]. Both are the same type in PHP; the only difference is how the keys look. array_is_list() returns true when the keys are 0, 1, 2 in order.
Use in_array($needle, $array, true) to look for a value, and array_key_exists($key, $array) to look for a key. Pass true as the third argument of in_array() for a strict comparison, so that the string '1' does not match the integer 1. array_key_exists() also finds keys whose value is null, which isset() does not.
array_filter() removes elements but does not renumber the rest, so the original keys stay. Filtering [1, 2, 3, 4, 5, 6] for even numbers leaves the keys 1, 3 and 5. Wrap the result in array_values() when you need a clean list that starts at 0.
Use array_map() to turn every element into a new value, array_filter() to keep only the elements that pass a test, and array_reduce() to boil the whole array down to one value such as a sum. They take a callback and return a result without changing the original array.
They are PHP 8.4 helpers that take a callback. array_find() returns the first value that passes the test (or null), array_find_key() returns its key, array_any() returns true if at least one element passes, and array_all() returns true only if every element passes. Before PHP 8.4 you wrote a foreach loop for the same job.
Use sort() or rsort() for a plain list when you do not need the old keys. Use asort() or arsort() to sort by value and keep the keys, and ksort() or krsort() to sort by key. Use usort(), uasort() or uksort() when you need your own comparison rule.