A PHP array stores a list of values under keys. Create one with square brackets, $fruits = ['apple', 'banana', 'cherry'];, and read an element by its index, $fruits[0]. Indexes start at 0, and count($fruits) gives the number of elements.
Every PHP array is an ordered map: each value sits under a key. When you do not choose the keys, PHP numbers them 0, 1, 2... and you get an indexed array. When you choose them yourself (['name' => 'Ada']), you get an associative array. Both are the same array type.
Create an array
Square brackets are the usual syntax. array() is the older spelling and produces exactly the same thing. range() builds a list of numbers or letters, and array_fill() repeats one value.
A PHP array can hold values of different types at once, including other arrays. An array of arrays is a multidimensional array, the usual way to hold rows of data.
Get the first and last element
PHP has no negative indexes. $arr[-1] does not mean "the last one": it looks up a key named -1 and, when there is none, PHP prints Warning: Undefined array key -1. Use array_key_first() and array_key_last(), which also work when the keys do not start at 0.
end($queue) also returns the last value, but it moves the array's internal pointer as a side effect, so the key-based version is the clearer choice.
Print an array
echo $arr does not work: it prints the word Array and a warning. Pick the output that fits the job:
print_r($arr)for a readable dump while debugging.var_dump($arr)to also see each value's type and length.implode(', ', $arr)to turn the values into one string for display.json_encode($arr)to send the array to JavaScript or an API.
The print_r and var_dump page covers the formatting options, including printing an array inside HTML.
Loop over an array
foreach is the loop made for arrays: it hands you each value, and the key too if you ask for it. A for loop with an index also works for indexed arrays whose keys run from 0 without gaps.
Arrays are copied, not shared
Assigning an array to another variable, or passing it to a function, gives a copy. Changing the copy leaves the original alone. Add & to the parameter when a function should change the caller's array.
Array functions by task
PHP ships more than 70 array functions. These are the ones you will use most, each with its own page:
| Task | Function |
|---|---|
| Add to the end | array_push() or $arr[] = |
| Number of elements | count() |
| Check if a value exists | in_array() |
| Find the key of a value | array_search() |
| Check if a key exists | array_key_exists() |
| Combine arrays | array_merge() |
| Transform every value | array_map() |
| Keep some values | array_filter() |
| Sort | sort(), usort() and friends |
| Remove an element | unset(), array_splice() |
| Take a part | array_slice() |
| Remove duplicates | array_unique() |
| String to array and back | explode(), implode() |
Keys that silently merge
Array keys are either integers or strings, and PHP converts some keys for you. A string that looks like a whole decimal number, such as "1", becomes the integer 1, so $a["1"] and $a[1] are the same slot. "01" and "1.5" stay strings. This is why an array built from form input or a CSV can end up with fewer elements than you expected.
Frequently Asked Questions
How do I create an array in PHP?
Use square brackets: $colors = ['red', 'green', 'blue'];. An empty array is $items = [];. The older array('red', 'green') syntax does the same thing and still works.
How do I print an array in PHP?
echo cannot print an array (it prints the word Array and a warning). Use print_r($arr) for a readable dump, var_dump($arr) to also see types, or echo implode(', ', $arr) to print the values as one line.
How do I get the last element of an array in PHP?
Use $arr[array_key_last($arr)] (PHP 7.3+) or end($arr). PHP has no negative indexes: $arr[-1] looks for a key named -1, which usually does not exist.
Are PHP arrays passed by value or by reference?
By value. $b = $a; gives $b its own copy, so changing $b does not change $a. To modify the caller's array inside a function, declare the parameter as a reference: function add(array &$list).
What is the difference between an indexed and an associative array in PHP?
They are the same type. An indexed array has integer keys 0, 1, 2... that PHP assigns for you; an associative array uses keys you choose, usually strings like 'name'. One array can even mix both.