Deleting Elements From An Array in PHP The Easy Way

Deleting elements from an array in PHP is a common task, but the easiest way to do this is by using PHP's unset() function. By specifying the array index that you would like to delete, as long as it exists, PHP will remove it.

$names = ['john', 'bob', 'paul'];
unset($names[1]);

# Becomes
Array
(
 [0] => john
 [2] => paul
)

By removing array index 1, PHP will delete "bob" from the array. That's because arrays are zero-based, meaning that the index references start from zero and not one. Using PHP's unset function does not reset the indexes, therefore, zero and two still exist in their same locations and can still be called upon later. If you were to remove array index one from the $names array and then later attempt to use it, PHP will trigger a Warning: Undefined array key. That's because the array index no longer exists in the array. We've written a huge guide to fixing the warning: Undefined array key error.

If you want to reset the array after deleting an element, instead of using unset, you can use array_splice. This function allows you to remove an element at a given index, and then PHP will reset the array back into an order index again. If we take the same original array and remove array index one again, "paul" will now move from array index 2 to 1. Start by passing the array you want to manipulate, the offset, and then the total number of elements you want to remove. In this case, we start at array index 1, and we want to delete only 1. Using this function works great with associative arrays.

array_splice($names, 1, 1);

# Becomes
Array
(
 [0] => john
 [1] => paul
)

You can convert an array's keys to numerical values we can use the array_values function to convert them. Then use array_splice as before to delete an element of the array.

$names = ['john' => 'CEO', 'bob' => 'IT Manager', 'paul' => 'Sales'];
print_r($names);

# Outputs
Array
(
 [john] => CEO
 [bob] => IT Manager
 [paul] => Sales
)

$names = array_values($names);
print_r($names);

# Outputs
Array
(
 [0] => CEO
 [1] => IT Manager
 [2] => Sales
)

If you don't know the key of an array you want to remove, you can search the array by value to remove a key that way. For that, we can use PHP's array_search. The array search function is a great way to find keys by a value. Once we have the array key name, we can unset it like before.

$names = ['john' => 'CEO', 'bob' => 'IT Manager', 'paul' => 'Sales'];

$theKey = array_search('CEO', $names);
unset($names[$theKey]);

We can also delete multiple array elements at once using PHP's array_diff and array_diff_key. Both achieve the same result but handle it in different ways. Using the first function, array_diff here we pass the array we're looking to amend, along with a list of array elements we want to delete. PHP will return a new array with those elements removed. Using this function, just like unset an array element, the array index isn't reset.

$names = ['john', 'bob', 'paul'];
$updatedNames = array_diff($names, ['bob']);

print_r($updatedNames);

# Outputs
Array
(
 [0] => john
 [2] => paul
)

We can similarly use array_diff_key to delete element items from PHP arrays. Passing two arrays to the function, the original and the removals, we can tell PHP which array key indices we want to be removed. Here, we're removing the first and second elements, leaving just one.

$names = ['john', 'bob', 'paul'];
$remove = [0, 1];
$updatedNames = array_diff_key($names, $remove);
print_r($updatedNames);

# Outputs
Array
(
 [2] => paul
)

Removing blank (empty) and zero array elements is another populate dev request. For that, we use array_filter. Array filter will strip out and delete elements for us when we don't provide a callback to the function following the rules of the empty function.

$names = [0, 'john', 'bob', 'paul', '', 'test' => 0];
$cleanedArray = array_filter($names);
print_r($cleanedArray);

# Outputs
Array
(
 [1] => john
 [2] => bob
 [3] => paul
)

We've written an ultimate PHP guide to arrays if you are looking for more details and guides on how arrays work in PHP.

Senior PHP developer with near two decades of PHP experience. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all