Menu

PHP Remove Element from Array: unset, array_splice

unset($array[$key]) removes an element by key, array_splice removes by position and renumbers, and array_diff removes by value. Learn each one, how to reindex afterwards, and the array_search pitfall that deletes the wrong element.

This page includes runnable editors - edit, run, and see output instantly.

To remove an element from a PHP array by its key, use unset($array[$key]). The element is gone, the other keys stay exactly as they were, and unsetting a key that does not exist is not an error.

Look at the second result: the keys are now 0, 2, 3. unset never renumbers.

Reindex the array after unset

A list with a gap in its keys causes trouble later: a for ($i = 0; ...) loop hits the missing index, and json_encode writes a JSON object instead of an array. array_values() gives you clean keys again.

Remove by position with array_splice

array_splice($array, $offset, $length) removes $length elements starting at position $offset, renumbers the keys, and returns what it removed. A negative offset counts from the end. It can also insert replacement elements in the same call.

array_splice works on positions, not keys: on an associative array it removes by place in the order, and it keeps string keys. The array_slice page compares it with array_slice, which copies a part without changing the original.

Remove an element by value

array_diff($array, [$value]) returns the array without every element equal to $value, keeping the keys of the rest. To remove only the first match, find its key with array_search and unset it.

array_diff compares values as strings, so array_diff([1, "1", 2], [1]) removes both the integer and the string. When the condition is more than "equals this value", use array_filter with a callback.

Remove the first or last element

array_shift() removes the first element and array_pop() removes the last. Both return the element they removed, which makes them handy for queues and stacks.

array_shift renumbers numeric keys from 0. array_pop does not need to, since the gap is at the end.

Remove several keys at once

unset accepts several elements in one call. To remove a list of keys you have in an array, flip it with array_flip() so the names become keys, then pass it to array_diff_key().

Common mistake: unset with array_search when the value is missing

array_search returns false when it finds nothing, and false used as an array key becomes 0. So this line deletes the first element whenever the value is not in the array:

Always store the result of array_search and compare it with !== false before you use it as a key. A plain if ($key) is not enough either: a match at index 0 is falsy too.

Frequently Asked Questions

How do I remove an element from an array by value in PHP?

To remove every occurrence, use array_diff($arr, ['value']). To remove only the first one, find its key and unset it, checking that it was found: $k = array_search('value', $arr); if ($k !== false) { unset($arr[$k]); }.

How do I reindex an array after unset?

Wrap it in array_values(): $arr = array_values($arr); renumbers the keys from 0. Alternatively remove the element with array_splice($arr, $index, 1), which renumbers for you.

How do I remove the first or last element of an array?

array_shift($arr) removes and returns the first element and renumbers the numeric keys. array_pop($arr) removes and returns the last element.

Can I unset an element inside a foreach loop?

Yes. foreach ($arr as $k => $v) { if ($v < 0) { unset($arr[$k]); } } is safe, because foreach loops over a copy of the array while you change the original. The keys keep their gaps, so call array_values() afterwards if you need a list.

Does unset on a missing key cause an error?

No. unset($arr['missing']) does nothing and raises no warning, so you do not need to check with isset first.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED