There are different ways to delete an array element, where some are more useful for some specific tasks than others.

Deleting a Single Array Element

If you want to delete just one single array element you can use unset() and alternatively array_splice().

By key or by value?

If you know the value and don't know the key to delete the element you can use array_search() to get the key. This only works if the element doesn't occur more than once, since array_search() returns the first hit only.

unset() Expression

Note: When you use unset() the array keys wonโ€™t change. If you want to reindex the keys you can use array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0 (the array remains a list).

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
          // โ†‘ Key of element to delete

Example Output:

[
    [0] => a
    [2] => c
]

array_splice() Function

If you use array_splice() the (integer) keys will automatically be reindex-ed, but the associative (string) keys won't change โ€” as opposed to array_values() after unset(), which will convert all keys to numerical keys.

Note: array_splice() needs the offset, not the key, as the second parameter; offset = array_flip(array_keys(array))[key].

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
                  // โ†‘ Offset of element to delete

Example Output:

[
    [0] => a
    [1] => c
]

array_splice(), same as unset(), take the array by reference. You donโ€™t assign the return values back to the array.

Deleting Multiple Array Elements

If you want to delete multiple array elements and donโ€™t want to call unset() or array_splice() multiple times you can use the functions array_diff() or array_diff_key() depending on whether you know the values or the keys of the elements to remove from the array.

array_diff() Function

If you know the values of the array elements which you want to delete, then you can use array_diff(). As before with unset() it wonโ€™t change the keys of the array.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = array_diff($array, ["a", "c"]);
                         // โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         // Array values to delete

Example Output:

[
    [1] => b
]

array_diff_key() Function

If you know the keys of the elements which you want to delete, then you want to use array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys wonโ€™t reindex.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);
                              // โ†‘           โ†‘
                              // Array keys of elements to delete

Example Output:

[
    [1] => b
]

If you want to use unset() or array_splice() to delete multiple elements with the same value you can use array_keys() to get all the keys for a specific value and then delete all elements.

array_filter() Function

If you want to delete all elements with a specific value in the array you can use array_filter().

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, static function ($element) {
    return $element !== "b";
    //                   โ†‘
    // Array value which you want to delete
});

Example Output:

[
    [0] => a
    [2] => c
]
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays_remove.asp
PHP Remove Array Items
In PHP, you can remove/delete array ... ยท unset() - removes the element associated with a specific key ยท array_diff() - remove items from an associative array ยท...
Top answer
1 of 16
3648

There are different ways to delete an array element, where some are more useful for some specific tasks than others.

Deleting a Single Array Element

If you want to delete just one single array element you can use unset() and alternatively array_splice().

By key or by value?

If you know the value and don't know the key to delete the element you can use array_search() to get the key. This only works if the element doesn't occur more than once, since array_search() returns the first hit only.

unset() Expression

Note: When you use unset() the array keys wonโ€™t change. If you want to reindex the keys you can use array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0 (the array remains a list).

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
          // โ†‘ Key of element to delete

Example Output:

[
    [0] => a
    [2] => c
]

array_splice() Function

If you use array_splice() the (integer) keys will automatically be reindex-ed, but the associative (string) keys won't change โ€” as opposed to array_values() after unset(), which will convert all keys to numerical keys.

Note: array_splice() needs the offset, not the key, as the second parameter; offset = array_flip(array_keys(array))[key].

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
                  // โ†‘ Offset of element to delete

Example Output:

[
    [0] => a
    [1] => c
]

array_splice(), same as unset(), take the array by reference. You donโ€™t assign the return values back to the array.

Deleting Multiple Array Elements

If you want to delete multiple array elements and donโ€™t want to call unset() or array_splice() multiple times you can use the functions array_diff() or array_diff_key() depending on whether you know the values or the keys of the elements to remove from the array.

array_diff() Function

If you know the values of the array elements which you want to delete, then you can use array_diff(). As before with unset() it wonโ€™t change the keys of the array.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = array_diff($array, ["a", "c"]);
                         // โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         // Array values to delete

Example Output:

[
    [1] => b
]

array_diff_key() Function

If you know the keys of the elements which you want to delete, then you want to use array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys wonโ€™t reindex.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);
                              // โ†‘           โ†‘
                              // Array keys of elements to delete

Example Output:

[
    [1] => b
]

If you want to use unset() or array_splice() to delete multiple elements with the same value you can use array_keys() to get all the keys for a specific value and then delete all elements.

array_filter() Function

If you want to delete all elements with a specific value in the array you can use array_filter().

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, static function ($element) {
    return $element !== "b";
    //                   โ†‘
    // Array value which you want to delete
});

Example Output:

[
    [0] => a
    [2] => c
]
2 of 16
1429

It should be noted that unset() will keep indexes untouched, which is what you'd expect when using string indexes (array as hashtable), but can be quite surprising when dealing with integer indexed arrays:

$array = array(0, 1, 2, 3);
unset($array[2]);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [3]=>
  int(3)
} */

$array = array(0, 1, 2, 3);
array_splice($array, 2, 1);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */

So array_splice() can be used if you'd like to normalize your integer keys. Another option is using array_values() after unset():

$array = array(0, 1, 2, 3);

unset($array[2]);
$array = array_values($array);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ php โ€บ deleting elements from an array in php
Deleting elements from an array in PHP | Sentry
If you want to delete an element from an array but you only know its value, you can use array_search() to find the key of the element, and then use unset() to remove the key-value pair.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.array-splice.php
PHP: array_splice - Manual
Note that the function does not use a reference to the original array but returns a new array (I see absolutely no reason how the performance would be increased by using a reference when modifying an array through PHP script code). ... To remove elements from an array, based on array values: <?php $i_to_remove=array(); foreach($array_to_prune as $i=>$value){ if(cond_to_delete($value)) $i_to_remove[]=$i; } foreach($i_to_remove as $j=>$i) array_splice($array_to_prune,$i-$j,1); ?>
๐ŸŒ
Uptimia
uptimia.com โ€บ home โ€บ questions โ€บ how to delete an element from an array in php?
How to Delete an Element from an Array in PHP?
May 20, 2024 - Learn how to remove an element from an array in PHP using the unset() function or array_splice(). Discover efficient ways to delete array elements by value or index.
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Delete element from within an array - PHP - SitePoint Forums | Web Development & Design Community
September 25, 2011 - Iโ€™ve seen PHP do bizarre things if you unset an element - particularly the element that will be next - in a foreach. Now granted, this behavior may have be fixed, but I long ago made it a point to avoid calling unset on the object Iโ€™m iterating on (I still do it when I absolutely need to, but that code gets extra heavy testing as a result). ... It was slice I was looking for. It was just confusing because there are about thirty array functions.
๐ŸŒ
Medium
medium.com โ€บ @aliusama.dev โ€บ deleting-an-element-from-an-array-in-php-9676a3438d0f
Deleting an Element from an Array in PHP | by AliUsama Dev | Medium
January 26, 2024 - $key = array_search("valueToRemove", $array); if ($key !== false) { unset($array[$key]); } Pros: โ€” Removes elements based on values when the key is unknown.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ easy way to remove all elements of an associative array with specified key?
r/PHPhelp on Reddit: Easy way to remove all elements of an associative array with specified key?
August 21, 2019 -
I have an array such as:

[0] => [
    "id" => 568,
    "first name" => "Greg",
    "last name" => "Vinall"
],
[1] => [
    "id" => 6,
    "first name" => "Fred",
    "last name" => "Nurd"
],
[2] => [
    "id" => 56,
    "first name" => "Mary",
    "last name" => "Quant"
]

I'd like to delete all elements whose key is "id", so that each array will only contain "first name" and "last name" keys and their associated values.

Is there a neat way to accomplish this, using a PHP array function?

To further the problem, I have another array: ["id" => "hidden, memberSource", "first name" => "member" ]

It is from this array that I find that it is the 'id" array that I want to remove because it contains the value "hidden".

Thanks.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-delete-an-element-from-an-array-in-php
How to delete an Element From an Array in PHP ? - GeeksforGeeks
July 23, 2025 - The unset() function removes a specified element from an array. It takes the element as a parameter and unsets it without re-indexing other elements, thus preserving the original keys in the array.
๐ŸŒ
Exakat
exakat.io โ€บ home โ€บ removing a value in an array, six methods
Removing a value in an array, six methods - Exakat
February 17, 2025 - arrayfilter() is the older cousin of arraydiff() : instead of making a direct comparison between values in two array, it applies a closure on each element, and keep the one that are true. Here, the closure is very simple, but in real life, it is convenient for complex calculations. Closure, use expression and arrow functions are all possible here. <?php // with use $c = array_filter($a, function($x) use ($b) { return $x !== $b;}); // without use $c = array_filter($a, function($x) { return $x !== 'b';}); // without fn $c = array_filter($a, fn($x) =&gt; $x !== 'b';); ?>
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ remove-specific-element-from-an-array-in-php
Remove Specific Element from an Array in PHP - GeeksforGeeks
July 23, 2025 - The unset() function is used to remove an element from an array by its key. This method works well when you know the key of the element to be removed.
๐ŸŒ
SourceBae
sourcebae.com โ€บ home โ€บ deleting an element from an array in php
Deleting an element from an array in PHP - SourceBae
August 21, 2025 - The unset() function in PHP is used to delete elements from arrays or unset variables. When unset() is applied to an array element, the element is removed, and the array index is reorganized.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ removing-array-element-and-re-indexing-in-php
Removing Array Element and Re-Indexing in PHP - GeeksforGeeks
July 11, 2025 - In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically.
๐ŸŒ
Quora
quora.com โ€บ Which-function-is-used-to-remove-or-delete-elements-from-an-array-in-PHP
Which function is used to remove or delete elements from an array in PHP? - Quora
Answer (1 of 3): Here are two ways to remove elements from arrays in PHP 1- unset() removes the element by its index [code]$arr = ["one", "two", "two", "three", "three", "four"]; unset($arr[2]); // remove element of index 2 [/code]output: [code]$arr = ["one", "two", "three", "three", "four"];...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ different-ways-to-delete-an-item-from-an-array-in-php
Different Ways to Delete an Item From an Array in PHP - GeeksforGeeks
July 23, 2025 - Example: The unset() function removes the element at index 2 and the keys are not reindexed, which might be important in some use cases. ... <?php $arr = array(1, 2, 3, 4, 5); // Removes the element at index 2 unset($arr[2]); print_r($arr); ?>
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-delete-an-element-from-an-array-in-php.php
How to Delete an Element from an Array in PHP
The following example shows how to delete an element from an associative array and numeric array. ... <?php $arr1 = array("a" => "Apple", "b" => "Ball", "c" => "Cat"); unset($arr1["b"]); // RESULT: array("a" => "Apple", "c" => "Cat") $arr2 = array(1, 2, 3); unset($arr2[1]); // RESULT: array(0 => 1, 2 => 3) ?>