How to Delete An Element From An Array In PHP

Avatar

By squashlabs, Last Updated: Nov. 16, 2023

How to Delete An Element From An Array In PHP

Deleting an element from an array in PHP can be achieved using several methods. In this answer, we will explore two common approaches.

Method 1: Using the unset() function

The unset() function is a built-in PHP function that can be used to remove elements from an array. Here's how you can use it to delete an element from an array:


In the code snippet above, we have an array called $fruits containing four elements. To delete the element at index 2 (which is 'orange'), we use the unset() function with the array element as the parameter. After deleting the element, we use the array_values() function to reindex the array and remove any gaps in the array indices caused by the deletion. Finally, we print the modified array using the print_r() function.

Related Article: PHP vs Python: How to Choose the Right Language

Method 2: Using array_splice()

Another way to delete an element from an array is by using the array_splice() function. This function allows you to remove a portion of an array and replace it with something else. In this case, we will remove a single element from the array.


In the code snippet above, we have the same array $fruits as in the previous example. To delete the element at index 2 (which is 'orange'), we use the array_splice() function with the array, start index, and length as parameters. The start index specifies the position from which to start deleting elements, and the length specifies the number of elements to delete. In this case, we delete only one element. Finally, we print the modified array using the print_r() function.

Best Practices

When deleting an element from an array in PHP, it is important to consider the following best practices:

1. Check if the element exists: Before deleting an element, it is a good practice to check if it exists in the array. This can be done using the isset() function or the array_key_exists() function.

2. Reindex the array: After deleting an element, the array indices may have gaps. To ensure a consistent array structure, it is recommended to reindex the array using the array_values() function, as shown in the first example.

3. Handle the return value: The unset() function does not return a value, while the array_splice() function returns an array containing the removed elements. It is important to handle the return value appropriately based on your requirements.

Alternative Ideas

Apart from the methods mentioned above, there are other ways to delete an element from an array in PHP. Some alternative ideas include:

1. Using the array_diff() function: The array_diff() function can be used to remove specific elements from an array by comparing it with another array containing the elements to be removed.

2. Using a loop: You can iterate over the array using a loop, check for the element you want to delete, and remove it using the unset() function or by reconstructing a new array without the element.

You May Also Like

Harnessing Laravel WebSockets for Real-time Features

Table of Contents Example 1: Building a Real-time Chat RoomExample 2: Real-time Dashboard with Live Data UpdatesBenefits of Using Laravel Echo for B… read more

Preventing Redundant MySQL Queries in PHP Loops

Minimizing redundant MySQL queries within PHP loops is essential for optimizing code performance. This article explores various techniques, such as q… read more

How to Fix the 404 Not Found Error in Errordocument in PHP

A simple guide to resolving the 404 Not Found Error when using Errordocument in PHP. Learn how to check file paths and permissions, review web server… read more

How To Add Elements To An Empty Array In PHP

Adding elements to an empty array in PHP is a common task for developers. This article provides step-by-step instructions on how to do it using two d… read more

How To Fix the “Array to string conversion” Error in PHP

Table of Contents Reasons for the ErrorPossible Solutions1. Concatenating an Array with a String2. Passing an Array as an Argument3. Echoing or Prin… read more

Processing MySQL Queries in PHP: A Detailed Guide

Learn how MySQL queries are returned in PHP and the steps involved in this process. Understand the syntax, retrieve and access the result set, displa… read more

How to Work with PHP Arrays: a Complete Guide

Learn how to work with arrays in PHP, from basic concepts to advanced techniques. This complete guide covers everything you need to know, including s… read more

Tutorial: Building a Laravel 9 Real Estate Listing App

Step 1: Building a Laravel 9 Real Estate Listing App will become a breeze with this step-by-step tutorial. Learn how to create a powerful single-app … read more

How To Get The Full URL In PHP

Table of Contents Approach 1: Using Server VariablesApproach 2: Using PHP’s built-in http_build_url functionReasons for AskingAlternative Ideas and … read more

How To Make A Redirect In PHP

Creating a redirect in PHP is essential for web developers. This step-by-step guide will teach you how to use the header() function and HTTP status c… read more