How to delete an element from an array in php?

When deleting an element from an array in PHP, a good function to use is the unset function. Here is an example of its usage:

An example of using unset to delete an element from an array:


$anArray = array("X", "Y", "Z");

unset($anArray[0]);

//'dumps' the content of $anArray to the page:
var_dump($anArray);  

The output of the var_dump function will be:

array(2) { [1]=> string(1) "Y" [2]=> string(1) "Z" }

Unset leaves all of the index values the same after an element is deleted

In our example above, the $anArray array will have values of “Y” and “Z” at indices of 1 and 2, respectively, after the element “X” is deleted using the unset function. This means that the indices for the other elements were not changed to adjust for the fact that the very first element (“X”) was deleted.

This would also mean that if you delete an element in the very middle of an array using unset then it would leave a gap in that array. Suppose we have this code:


$anArray = array("V", "W", "X", "Y", "Z");

unset($anArray[2]);

//'dumps' the content of $anArray to the page:
var_dump($anArray);  

This would output the following:

array(4) { [0]=> string(1) "V" [1]=> string(1) "W" 
[3]=> string(1) "Y" [4]=> string(1) "Z" }

Note in the output above, there is now no index # 2 – there is 0, 1, 3, and 4. Not having the continuous index values could potentially be a negative drawback. So what are you alternatives? Well, you could use a function called array_splice instead.

Using array_splice to delete an element from an array

The array_splice function is used to take one part of an array and replace it with some other contents. It can also be used to delete an element in an array. Here is an example and explanation of how to use array_splice to delete an element:

$anArray = array("V", "W", "X", "Y", "Z");

/*The 2 represents the offset - which basically
means move 2 positions from the  beginning of the
array and that will take us to the "X" element.  The 1 
represents the length of the array that you want to 
delete.  Since we just want to delete 1 element, we
set the length parameter to 1.

And, since we are not replacing that element with anything
- we do just want to delete it - we leave the 4th parameter (
which is optional) blank
*/

array_splice($anArray, 2, 1);
var_dump($anArray);

Running the code above will return us this:

array(4) { [0]=> string(1) "V" [1]=> string(1) "W"
[2]=> string(1) "Y" [3]=> string(1) "Z" }

So, using array_splice will set the index values back to their correct order, and we will be good to go again. There is however an assumption here that we should point out – the array_splice function accepts a value for the offset, not the index. Theoffset that we used in the example above happened to be equal to the index value. This is the case when the array we are dealing with already has a continuous integer index value, but if the array has been changed for whatever reason before array_splice is used, that may not always be the case.

Using the array_values and unset functions to delete an element from an array

If you use the array_values function right after the unset function, then you can set the index values back to their normal values, without any gaps in between the numbers. An example will help clarify:

Example of using array_values and unset to delete an element from an array:

$anArray = array("V", "W", "X", "Y", "Z");

/*
This will cause index 2 to go missing,
so the array indices of $anArray will be
0,1,3,4 - obviously the 2 is missing
*/

unset($anArray[2]);

/*
array_values will take an array as an input
and then take the array values (not the keys,
but just the values), and numerically index those values
into a new array.

This means that array_values will essentially re-index
the array that is given as an input, which restores the indices
to the correct order of 0,1,2, and 3:
*/

$anArray = array_values($anArray);

//'dumps' the content of $anArray to the page:
var_dump($anArray);  

This will now output:

array(4) { [0]=> string(1) "V" [1]=> string(1) "W" 
[2]=> string(1) "Y" [3]=> string(1) "Z" }

The array_values function will replace non-numeric key indices with numeric values

One thing we should point out about the array_values function is that if the key’s are non-numeric then they will be replaced with numeric values anyways. So, if you have an array that uses strings as indices (which is basically a hashtable), then array_values will remove those strings and replace them with numerical values. This is something you should definitely watch out for if you do decide to use the array_values function.

Now you have seen the different options that you have available to you when deleting an element from an array in PHP. And you are also aware of any potential side effects – which method you choose to use is entirely up to you.

How to delete an element in an array if you only know the value in PHP

You may want to delete an element in an array for which you only know the value (but not the corresponding key). In that scenario, you will have to search the array for the value you want first in order to get the corresponding key. You can use the array_search function to do that – it will simply take the array you want to search along with the value you want to search for as the parameters, and will return the corresponding key if it is found. Then, you can use the unset function to remove the element as before.

And finally, here is an example of how to do it:

An example of deleting an element in an array if you only know the value in PHP

$key = array_search($valueToSearch,$arrayToSearch);
if($key!==false){
    unset($array[$key]);
}

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

One thought on “How to delete an element from an array in php?”

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.