How to Delete An Element From An Array In PHP

Avatar

By squashlabs, Last Updated: November 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: How to Work with Arrays in PHP (with Advanced Examples)

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.

Related Article: How To Make A Redirect In PHP

You May Also Like

How to Work with Arrays in PHP (with Advanced Examples)

This article provides a guide on how to work with arrays in PHP, covering both basic and advanced examples. It also includes real-world examples, best practices, and an... 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 codes to efficiently... 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 different methods:... read more

How To Convert A String To A Number In PHP

Converting a string to a number in PHP is a common task that every developer needs to know. This article provides simple explanations and clear examples for six... read more