How to Delete an Element From an Array in PHP?

Last updated on August 15, 2023

In PHP, we can delete an element from an array in multiple ways. In this article, we will be learning different patterns using PHP built-in functions for removing the element from an array.

1. unset() Function

The unset() is a built-in function that unsets the variable. It can be used to remove an element from the array.

$fruits = [0 => "apple", 1 => "banana", 2 => "cherry", 3 => "strawberry"];
$removeIndex = 1;

In this example, we defined an array called $fruits with four elements. Each element is assigned a specific index: “apple” is at index 0, “banana” is at index 1, “cherry” is at index 2, and “strawberry” is at index 3.

We have another variable $removeIndex that stores the index which has to remove from the fruits array. At index 1, the fruits array has “banana”.

if(isset($fruits[$removeIndex])){
    unset($fruits[$removeIndex]);
}

We are using an if-statement to check whether the element at the specified index ($removeIndex) exists in the fruits array. If it does, the unset() function will be removing the element from the fruits array.

Let’s check the output of the fruits array after running the above if-statement. To check the modified fruits array, we can use print_r() function that simply prints the array.

print_r($fruits);

// Output: 

Array
(
    [0] => apple
    [2] => cherry
    [3] => strawberry
)

2. array_splice() Function

In this example, we are using the same fruits array that we used in the previous example. But this time we are removing index 2 which has “cherry”. Look at the fruits array and index variable that stores the value needed to remove.

$fruits = [0 => "apple", 1 => "banana", 2 => "cherry", 3 => "strawberry"];
$removeIndex = 2;

Next, we need an if-statement along with PHP built-in function array_splice() to remove index 2 from the fruits array.

if(isset($fruits[$removeIndex])){
    array_splice($fruits, $removeIndex, 1);
}

The if-statement checks if the element at the specified $removeIndex exists in the $fruits array by using isset() function. If it does, the array_splice() function is used to remove the element from the array.

The array_splice() function takes three arguments:

  • First: array to modify ($fruits)
  • Second: The starting index from which to remove elements ($removeIndex)
  • Third: The number of elements to remove (in this case, 1 element)

When we execute the above if-statement the “cherry” element at index 2 will be removed from the array. To check the output of the modified fruits array, we can use the print_r() function.

print_r($fruits);

// Output: 

Array
(
    [0] => apple
    [1] => banana
    [2] => strawberry
)
How to Delete an Element From an Array in PHP?


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: PHP,