Skip to content
Home » Remove Element from PHP Array: 5+ Easy Code Examples (2023)

Remove Element from PHP Array: 5+ Easy Code Examples (2023)

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.

How to remove an Element from a PHP Array

PHP offers several functions to help remove an element from a PHP array: unset, array_pop, array_unshift, and array_splice are the most popular. The array_splice function is the most versatile, and unset can remove an element from anywhere. Use the array_pop & array_unshift functions to remove from the arrays’ beginning or end.

Check out the remainder of the article to see real-world code examples.

Article Highlights

  1. array_splice is the most versatile option to remove elements from arrays
  2. unset can remove elements from anywhere in PHP arrays
  3. array_pop is a good option to remove elements from end of arrays
  4. array_shift can be used to remove elements from the beginning of arrays

Remove Elements from Arrays PHP Code Example

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.

Remove Element from Associative Array using unset() in PHP

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.

Unset PHP Code Example to Remove Associative Array Element

$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.

Remove Element from Associative 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.

array_pop Code Example to Remove Element from PHP Associative Array

$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.

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 elements from an associative array in PHP using array_splice.

Remove element from Associative Array PHP using array_splice()

The PHP array_splice is the most common function to remove an element from an array in PHP. It will 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.

How to Remove Elements from Associative

There are 4 common functions to remove elements from an array in PHP. Unset, array_pop, array_shift & array_splice. Unset & array_splice are the most commonly used because you can remove an element at any point in an array. array_pop and array_shift allows you to remove the elements from the beginning and end of the associative array.

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.

PHP Fundamentals Recommendations

This article is part of our content on PHP Fundamentals. It includes the core concepts that build upon the foundation of writing high-quality PHP code. If you are looking to grow your PHP development abilities. Check out the following recommended affiliate resources.

We do make a commission if you do choose to buy through our links. It is one of the ways that help support our mission here at FuelingPHP.

Book: Fundamentals of Web Development

This book is for you if you are starting to learn how to build websites. It is more than just an “intro to programming” book. You will learn the concepts and tips on what goes into creating a high-quality website. Today’s websites are more than text on a screen. They are highly complex applications that encourage user experience. Learn the fundamentals of good web development with this book.

Check it out on Amazon

Book: Programming in PHP (O’Reilly)

O’Reilly should not need any introduction. They are the top publishers when it comes to books on programming and technology. This book fits well within their vast library. If you are newer to the PHP language or want to keep a solid reference by your side. I highly recommend this book for your collection.

Check it out on Amazon

Book: Design Patterns in PHP

I highly recommend this book to any intermediate-level web developer. It takes the theories and best practices of writing high-quality code via design patterns and applies them to PHP. It is a great resource to take your career to the next level

Check it out on Amazon

Video Course: PHP Fundamentals (Pluralsight)

Want to quickly learn PHP? This PHP Fundamentals course is ideal for beginner PHP developers. It is a deep dive into the concepts, structures and well “fundamentals” of PHP development. It includes high-quality video & interactive resources that teach you really fast. I highly recommend this if you are getting started in your PHP journey.

Click here for a 10-day free trial to Pluralsight

Complete Learning Path: Web Development (Pluralsight)

You should definitely check out this learning path from Pluralsight. They have a huge list of video courses, training, and interactive lessons on growing your web development career. You get access to the full library of hundreds of resources for a single monthly subscription. It truly is like Netflix for your career.

Click here to see details (10-day free trial included)

Want more reviews? Check out our HUGE list of Beginner Web Development Books We’ve Researched and Reviewed for you.

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

Learn the Fundamentals of Good Web Development

Please take a moment and sign up for our free email course on the fundamentals of good web development. Every week is packed with a roundup of articles on our site and from around the web, where we go deep into developing exceptional web applications. We have meetups, code reviews, slack chats, and more.

Click here to get started