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
]
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)
} */
๐ŸŒ
Plus2Net
plus2net.com โ€บ php_tutorial โ€บ array_unset.php
Deleting key & value element of array by using unset command in PHP
February 5, 2000 - This unset command takes the array key as input and remove that element from the array. After removal the associated keys and values ( of other balance elements ) does not change.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays_remove.asp
PHP Remove Array Items
To remove items from an associative array, you can use the unset() function. Specify the key of the item you want to delete.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.unset.php
PHP: unset - Manual
A sample how to unset array elements from an array result coming from a mysql request. In this sample it is checking if a file exists and removes the row from the array if it not exists. <?php $db->set_query("select * from documents where document_in_user = 0"); //1 $documents = $db->result_to_array($db->get_result()); //1 foreach ($documents as $key => $row) { //2 $file = "uploads/".rawurldecode($row['document_name']); if ( file_exists ( $file ) == FALSE ) { unset($documents[$key]); //3 } } $documents = array_values($documents); // reindex the array (4) ?> variables: mysql table = documents, array = $documents array key (index) = $key array row (record sort of speak) = $row explanation: 1.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ php tutorial โ€บ php unset array
PHP unset Array | Delete an Array Element using unset Fumction
April 5, 2023 - So, in this example code, we will try to remove that notice message. ... <?php $array1 = array(1 => "Red", 3=>"Green", 2=>"Blue"); echo "<pre>"; echo "Array elements are:<br>"; print_r($array1); unset($array1); // unset the complete array.
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-delete-an-array-element-based-on-key-in-php
How to Delete an Array Element based on Key in PHP? - GeeksforGeeks
July 11, 2025 - ... <?php // PHP program to delete an array // element based on index // Declare arr variable // and initialize it $arr = array('G', 'E', 'E', 'K', 'S'); // Display the array element print_r($arr); // Use unset() function delete // elements ...
๐ŸŒ
Purpleturtlecreative
purpleturtlecreative.com โ€บ home โ€บ blog โ€บ how to recursively unset array keys and object properties in php
How to Recursively Unset Array Keys and Object Properties in PHP
September 10, 2024 - */ function deep_unset_prop( ... );// See the result.Code language: PHP (php) Use this function when you want to only unset a specific key in every associative array....
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Koenwoortman
koenwoortman.com โ€บ php-remove-keys-from-associative-array
Remove key and value from an associative array in PHP
To remove a key and its respective value from an associative array in PHP you can use the unset() function.
๐ŸŒ
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.

๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-delete-php-array-element-by-value-not-key.php
How to Delete PHP Array Element by Value Not Key
<?php // Sample indexed array $array1 = array(1, 2, 3, 4, 5); // Search value and delete if(($key = array_search(4, $array1)) !== false) { unset($array1[$key]); } print_r($array1); echo "<br>"; // Sample eassociative array $array2 = array("a" => "Apple", "b" => "Ball", "c" => "Cat"); // Search value and delete if(($key = array_search("Cat", $array2)) !== false) { unset($array2[$key]); } print_r($array2); ?>
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ php-array-remove-keys-keep-values-exampleexample.html
PHP Array Remove Keys and Keep Values Example - ItSolutionstuff.com
May 14, 2024 - we can make it done using array_values function of php array. array_values() will re-create array with new keys so basically array_values function will create new array with new key like default 0 1 2 etc.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-remove-a-key-and-its-value-from-an-associative-array-in-php
How to remove a key and its value from an associative array in PHP ? - GeeksforGeeks
June 7, 2024 - The array_filter function can be used to filter elements of an array using a callback function. This approach allows you to remove specific keys by returning false for those keys in the callback function.
๐ŸŒ
Learn Code Web
learncodeweb.com โ€บ home โ€บ delete an array element based on a key in php?
Delete an array element based on a key in PHP? - Learn Code Web
January 25, 2021 - So in this way you can delete any element based on a key in PHP. If you want to remove array elements with multiple keys you can do that. For that, you need to consider the below function. function removeRecursive($inputArray,$delKey){ if(is_array($inputArray)){ $moreKey = explode(",",$delKey); foreach($moreKey as $nKey){ unset($inputArray[$nKey]); foreach($inputArray as $k=>$value) { $inputArray[$k] = removeRecursive($value,$nKey); } } } return $inputArray; } //Pass multiple keys with comma saperated.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php unset() function
PHP unset() Function - Scaler Topics
September 29, 2023 - In this example, the unset() function is used to remove the array element with key 1 from the $array.
๐ŸŒ
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
<?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) ?>
๐ŸŒ
Tech Drill Down
techdrilldown.com โ€บ php-basics-all-the-ways-to-remove-a-key-from-a-php-array
PHP Basics: All the ways to remove a key from a PHP array
December 13, 2022 - In PHP, there are several ways to remove a key from an array. Here are some examples: Using the unset() function: $array = ['a', 'b', 'c']; unset($array[1]); This code removes the element with the key 1 from the $array array.