How to delete an array element based on key?

How to delete an array element based on key?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to delete an array element based on key?

In PHP, removing a specific element from an array based on its key is a frequent requirement, but the approach changes significantly depending on whether you are dealing with indexed arrays, associative arrays, or multidimensional structures. The provided source material illustrates a common pitfall: attempting to remove a key from a nested array structure while preserving the surrounding elements without causing index gaps or data loss.

Consider a multidimensional array where each top-level element contains its own sub-array. If you simply unset an index at the root level, PHP will automatically re-index the remaining elements using array_values() logic if you were to iterate over them later, or it might leave "holes" in the memory map of the array. In the example provided, we have a structure where index [1] holds a sub-array with keys ['open', 'center']. The goal is to remove that entire entry so that index [0] remains paired with a new index [2], effectively skipping the middle section entirely.

The most direct and idiomatic way to achieve this in modern PHP (7.4+) is using the array_filter() function combined with an arrow function, or simply using unset(). However, unset() is the precise tool for deleting a specific key without altering the keys of subsequent elements.

To remove the element at root index 1 specifically, you can use:

$array = [
    0 => ['hello', 'open'],
    1 => ['good', 'center'],
    2 => ['hello', 'close']
];

unset($array[1]);

print_r($array);
// Output preserves the gap at index 1:
// Array ( [0] => Array (...) [2] => Array (...) )

This maintains the original integer keys [0] and [2]. If you subsequently iterate over $array using a foreach loop, PHP treats the array as an associative array because of the missing index. This is often the desired behavior when skipping specific entries without re-indexing.

If your goal is to remove an element from within one of these nested sub-arrays based on its internal key, the logic shifts slightly. You must access the parent array first and then apply unset() to the child:

// Removing 'center' from the second sub-array (which was at index 1)
if (isset($array[1])) {
    unset($array[1][1]); 
}

Here, $array[1][1] refers to the value 'center'. After this operation, the second sub-array becomes ['good'], while the outer gap at index [2] remains untouched until you decide to re-index.

A common mistake developers make is assuming that removing an element from a multidimensional array will automatically collapse the parent structure or clean up the internal keys of the remaining items. unset() does not recursively clean up keys within the value it removes; it only removes the top-level key. If you need to ensure that the sub-arrays themselves are re-indexed sequentially (e.g., converting [0, 1] back to [0, 1] after deletion), you must explicitly call array_values() on the affected sub-array.

unset($array[1]);
$array[1] = array_values($array[2]); // Merging and re-indexing is manual work

In many cases, specifically when using Laravel or similar frameworks, you might encounter collections that handle these operations more gracefully. If you are working with Eloquent models or Collections, you can use the reject() method to filter out items based on a condition, which effectively removes them from the collection while maintaining order. However, for raw PHP arrays and the specific task of deleting a key by integer index, unset() remains the standard, efficient solution.

It is also worth noting that if you are iterating over the array immediately after deletion, be aware that the loop variable will skip the removed index. For instance:

foreach ($array as $key => $value) {
    // If key 1 was unset, the loop jumps from 0 to 2
}

This behavior is technically correct but can lead to logic errors if your code expects a contiguous sequence of indices. Always verify whether you need a dense array (contiguous keys) or a sparse one (preserving original gaps). If you require a dense array, array_values($array) after the unset() operation will re-index everything starting from zero.

Our network