Skip to content
Home » Remove Elements from associative & multidimensional PHP Arrays

Remove Elements from associative & multidimensional PHP Arrays

Summary

You can use the unset, array_pop, array_splice & array_shift functions to delete an element from a PHP array. The right function to use depends on where the element exists in the array. Check out the article for more details.

Remove Elements from Arrays PHP – Code Snippet

Here’s a featured snippet of how to remove elements from an associative array in PHP.

$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
);
 
//array_pop will return the last element.
$employee = array_pop($employees_array);
 
print_r($employees_array);
 
 
/*
OUTPUT
Array
(
    [Employee#1] => Bob   
    [Employee#2] => Stacie
    [Employee#3] => Robert
    [Employee#4] => Anna  
)
*/

There are several methods to remove elements from associative arrays in PHP. These methods differ in terms of removing elements at different positions in an array. In this article, we’ll see different functions that come in handy for removing elements from an array. So, let’s jump straight to the article without any hassle.

Option1 – PHP Remove Element from Array using unset()

PHP unset function takes at least one variable as an argument and unsets it. This function is handy for removing elements from an array in PHP. All you need is to pass it to the function. Let’s see how it does so.

$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
//unset removes the element.
unset($employees_array["Employee#4"]);
 
print_r($employees_array);
 
 
/*
OUTPUT
Array
(
    [Employee#1] => Bob    
    [Employee#2] => Stacie 
    [Employee#3] => Robert 
    [Employee#5] => Matthew
    [Employee#6] => John   
)
*/

A plus point of unset is that it allows removing an element from any position. The drawback is that it won’t reindex numeric keys. However, we have literal key values, so reindexing isn’t a concern here.

Option2 – Remove Element from Array in PHP using array_pop()

PHP array_pop removes an element from the end of an array. The function is simplistic and the only argument is the array itself. It shortens an array by one element and returns the removed element. However, if the array is empty then it will return null. Let’s use it to remove element from associative array in PHP.

$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
);
 
//array_pop will return the last element.
$employee = array_pop($employees_array);
 
print_r($employees_array);
 
 
/*
OUTPUT
Array
(
    [Employee#1] => Bob   
    [Employee#2] => Stacie
    [Employee#3] => Robert
    [Employee#4] => Anna  
)
*/

So, the function does what is expected. Next, we will how to remove an element from the start of an array.

Option3 – Remove Element from Associative Array PHP using array_shift()

PHP array_shift removes an element from the beginning of an array. Similar to the array_pop function, it takes an array as an argument and returns the removed element. One important point is related to the key values. All the numeric keys will restart the counting from zero while the literal keys would remain intact.

Let’s reuse the same array as in the previous example.

$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
);
 
//array_pop will return the first element.
$employee = array_shift($employees_array);
 
print_r($employees_array);
 
 
/*
OUTPUT
   Array
(
    [Employee#2] => Stacie 
    [Employee#3] => Robert 
    [Employee#4] => Anna   
    [Employee#5] => Matthew
)
*/

The first element is removed and the literal keys here are not affected. In the next section, we will see how to remove element from associative array in PHP using array_splice.

Option4 – Remove element from Associative Array PHP using array_splice()

PHP array_splice is a panacea to the use case at hand. That’s because this function can remove elements from an array from any position. All you need is to plug in the right values for offset and length. That’s what we will see in the examples that follow. Let’s see this function in action.

1. Remove from the Middle of a PHP Array using array_splice()

We’ll consider the same array from the previous example but with six employees for this example. Let’s say we want to remove “Employee3” who is in the middle of the array. Using array_splice, the right offset value would be 2 because an array index starts at zero. The length would obviously be 1 because we’re removing one element only.

Let’s see it through the example using offset as 2 and length as 1.

$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
//array_splice modified the employee_array and returns the removed element.
$employee = array_splice($employees_array,2,1);
 
print_r($employees_array);
 
 
/*
OUTPUT
Array
(
    [Employee#1] => Bob    
    [Employee#2] => Stacie 
    [Employee#4] => Anna   
    [Employee#5] => Matthew
    [Employee#6] => John   
)
*/

The keys are preserved here but seem odd because “Employee3” is just skipped. Unfortunately, there’s nothing we can do about it. The only solution would be to use numeric keys as they are reindexed each time the function modifies the array.

2. Remove from the End of an Array using array_splice()

To remove an element from the end of an array, we would specify a -1 offset. A -1 offset means the last element of an array. The length can be omitted here. Let’s see how that works.

$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
//array_splice modified the employee_array and returns the removed element.
$employee = array_splice($employees_array,-1);
 
print_r($employees_array);
 
 
/*
OUTPUT
Array
(
    [Employee#1] => Bob
    [Employee#2] => Stacie
    [Employee#3] => Robert
    [Employee#4] => Anna
    [Employee#5] => Matthew
)
*/

3. Remove from the Start of an Array using array_splice()

Lastly, we see how to remove from the beginning of an array and we hope it won’t be difficult for you to guess the right offset and length value. 

$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
//array_splice modified the employee_array and returns the removed element.
$employee = array_splice($employees_array,0,1);
 
print_r($employees_array);
 
 
/*
OUTPUT
Array
(
    [Employee#2] => Stacie 
    [Employee#3] => Robert 
    [Employee#4] => Anna   
    [Employee#5] => Matthew
    [Employee#6] => John   
)
*/

Perfect!! The array_splice is a handy function to remove elements from an associative array in PHP.

Removing Elements from Associative or Multidimensional Arrays

We’ve seen different ways to remove elements from an array in PHP. PHP array_splice is more versatile in the sense that it can remove elements from any position, and it can also reindex numeric keys. The other functions, as we’ve seen, are more specialized. You can stick to the one you feel comfortable with.

We hope that you’ve enjoyed this article. Stay tuned for more at FuelingPHP.

Classes and Functions Mentioned

unset – (PHP 4, PHP 5, PHP 7, PHP 8) As the name suggests this function unsets a variable. It was introduced in PHP 4 and remains stable in the latest PHP.

array_pop – (PHP 4, PHP 5, PHP 7, PHP 8) A core PHP function to pop off the elements from the end of an array. It has been available in the early fourth PHP installation and has been a stable function until now.

array_shift – (PHP 4, PHP 5, PHP 7, PHP 8) A built-in PHP function to shift an element from the start of an array. This function has been available from the fourth to the eighth PHP installation.

array_splice – (PHP 4, PHP 5, PHP 7, PHP 8) This function removes and replaces a portion of an array. It is one of the most mature PHP functions available right off the bat.

Want to explore further about PHP arrays?

We have many fun articles related to PHP arrays. You can explore these to learn more about arrays in PHP.

How to split associative arrays in PHP

How to convert associative array to csv in PHP

Add to an array in PHP