To add and remove array elements in PHP, use array_push() or the [] shorthand to append, array_unshift() to prepend, array_pop() to remove the last element, array_shift() to remove the first, and unset() or array_splice() to remove an element at a given position. The php array push pop shift family changes the array you pass in, so the array is passed by reference.
Each function behaves slightly differently with keys, so knowing which one renumbers indexes and which one leaves gaps saves you from common bugs. The examples below use PHP 8.3 and also work on 8.4 and later.
[] and array_push()
array_unshift()unset()array_splice()[] and array_push()The shortest way to append a value is the empty-bracket syntax. It adds the value with the next free integer key.
array_push() accepts several values at once and returns the new element count. For a single value, $fruits[] = 'x' is preferred: it is a language construct, so it avoids a function call, and it is the idiomatic style. Use array_push() only when you add many values in one call.
Both forms above only work with automatic numeric keys. If you need a named key, assign it directly.
array_unshift()array_unshift() inserts one or more values at the beginning and returns the new length.
Numeric keys are renumbered from zero. String keys are kept. Prepending is slower than appending on large arrays because every existing element must be re-indexed, so avoid it inside tight loops over big data.
array_pop() removes and returns the last element. array_shift() removes and returns the first element and renumbers the remaining numeric keys.
Both return null when the array is empty, which is also what you get if the array held a null value, so check with count() or $stack === [] first when it matters.
A stack is last in, first out: use [] or array_push() with array_pop(). A queue is first in, first out: use [] with array_shift(). For large queues the SPL classes SplStack, SplQueue and SplDoublyLinkedList avoid the re-indexing cost.
unset()unset() removes an element by key. It does not renumber the array, so you end up with gaps.
If you want sequential keys again, wrap the result in array_values():
To remove by value instead of key, find the key first with array_search() and then unset it, or filter with array_filter().
array_splice()array_splice() removes a slice and optionally inserts replacement values in its place. It renumbers numeric keys and returns the removed elements.
The arguments are the array, the offset, the number of elements to remove and the replacement. A negative offset counts from the end.
| Function | Position | Returns | Renumbers numeric keys |
|---|---|---|---|
$a[] = $v | end | the assigned value (as an expression) | no, uses next key |
array_push() | end | new length | no, uses next keys |
array_unshift() | start | new length | yes |
array_pop() | end | removed value | not needed |
array_shift() | start | removed value | yes |
unset($a[$k]) | any key | nothing | no |
array_splice() | any offset | removed elements | yes |
array_pop() change the original array?Yes. The array is passed by reference, so the last element is removed from your variable and returned. If you need to keep the original, copy it first with a normal assignment, since PHP arrays are copied on write.
unset()?unset() only deletes the one key and leaves the rest alone. Call array_values() afterwards when you need a zero-based list again, for example before encoding to JSON, where gaps would turn a list into an object.
foreach?Appending to the array you are looping over does not extend the loop, because foreach iterates over a copy by default. Build a separate result array instead, which is clearer and safer.
The [] syntax. It has no function-call overhead and is the form most code uses. Use array_push() only for several values.
Append with [] or array_push(), prepend with array_unshift(), and take from the ends with array_pop() and array_shift(). Use unset() to delete a single key and accept the gap, or array_values() to close it. Reach for array_splice() when you need to insert or remove in the middle, and remember that it and the shift and unshift functions renumber numeric keys while unset() does not.