How to Remove Elements from Arrays in PHP

Like many things in PHP, there are multiple ways to approach removing elements from arrays. In this tutorial, we will learn several of these methods so you can decide which one suits resolving your situation best.

 

Remove First Element from Array Using array_shift()

The PHP array_shift() utility removes the first item from an array and decrements all the following element keys by one.

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

array_shift($fruit);

print_r($fruit);
Array ( [0] => strawberry [1] => apricot [2] => orange )

 

Remove the Last Element from Array Using array_pop()

The PHP array_pop() utility removes the last element from an array.

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

array_pop($fruit);

print_r($fruit);

 

Array ( [0] => apple [1] => strawberry [2] => apricot )

 

Remove Multiple Elements from an Array Using array_splice()

The array_splice() utility can remove multiple elements from an array starting at any position and add new elements. It also reindexes the keys but won't change associative keys.

 

array_splice() takes four arguments, the array, the offset (starting position), length (number of elements to remove) and new element(s) to add. The last two arguments are optional.

 

array_splice(array, offset [, length] [, elements]);

 

Let's try removing two elements from an array using array_splice():

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

array_splice($fruit, 1, 2);

print_r($fruit);
Array ( [0] => apple [1] => orange )

 

Since array indexes start at zero, the second and third elements are removed from the array in the above example.

 

If no length is supplied to array_splice() every element after the offset is removed:

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

array_splice($fruit, 1);

print_r($fruit);
Array ( [0] => apple )

 

A negative offset can be supplied to remove elements from the end of the array. The example below demonstrates removing the last two elements from an array:

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

array_splice($fruit, -2, 2);

print_r($fruit);
Array ( [0] => apple [1] => strawberry )

 

Elements added using array_splice() will be placed after the offset index. Let's try removing the first two elements from an array, then adding two new ones.

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

array_splice($fruit, 0, 2, ['peach', 'lemon']);

print_r($fruit);
Array ( [0] => peach [1] => lemon [2] => apricot [3] => orange )

 

Remove Array Elements without Reindexing Using unset()

The unset() utility removes elements from an array without reindexing it. Supply the key you want to delete like this:

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

unset($fruit[1]);

print_r($fruit);
Array ( [0] => apple [2] => apricot [3] => orange )

 

To reindex the array, use the array_values() utility and assign the result to the variable of the original array.

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

unset($fruit[1]);

$fruit = array_values($fruit);

print_r($fruit);
Array ( [0] => apple [1] => apricot [2] => orange )

 

Delete Array Elements by Value Using array_diff()

To delete multiple elements by their value, use the array_diff() utility. The first argument is the original array and the second is the elements to remove. array_diff() doesn't change the initial array so the result will need to be assigned to a variable.

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

$fruit = array_diff($fruit, ['orange', 'strawberry']);

print_r($fruit);
Array ( [0] => apple [2] => apricot )

 

 

array_diff() will not reindex keys; this will need to be done manually using array_values().

 

$fruit = ['apple', 'strawberry', 'apricot', 'orange'];

$fruit = array_diff($fruit, ['orange', 'strawberry']);

$fruit = array_values($fruit);

print_r($fruit);
Array ( [0] => apple [1] => apricot )

 

 

Conclusion

You now know how to remove elements from arrays in several different ways. The method you use will depend on the specific problem in hand.

array