How to Delete an Element from an Array in PHP

  1. Using unset() function to delete an element from an array in PHP
  2. Using array_splice() function to delete an element from an array in PHP
  3. Using array_diff() function to delete an element from an array in PHP

In this article, we will introduce methods to delete an element from an array in PHP.

  • Using unset() function
  • Using array_splice() function
  • Using array_diff() function

Using unset() function to delete an element from an array in PHP

The built-in function unset() is used to delete the value stored in a variable. It is only applicable to the local variables. It does not reflect its behavior on global variables. We can use this function to delete an element from an array. The correct syntax to use this function is as follows

unset($variableName);

It has a single mandatory parameter. The variable whose value we wish to delete is passed as a parameter to this function.

<?php
//Declare the array
$flowers = array(
                "Rose",
                "Lili",
                "Jasmine",
                "Hibiscus",
                "Tulip",
                "Sun Flower",
                "Daffodil",
                "Daisy");

unset($flowers[1]);
echo "The array is:\n";
print_r($flowers);
?>

This function can delete one value at a time. The name of the array along with the element index ($flowers[1]) is passed as a parameter. This function does not change the index values. The index values remain the same as they were before.

Output:

The array is:
Array
(
    [0] => Rose
    [2] => Jasmine
    [3] => Hibiscus
    [4] => Tulip
    [5] => Sun Flower
    [6] => Daffodil
    [7] => Daisy
)

As you could see, the index 1 is missing after we apply the unset function.

Using array_splice() function to delete an element from an array in PHP

The function array_splice() is used to add elements to an array or delete elements from an array. The correct syntax to use this function is as follows

array_splice($arrayName, $startingIndex, $numOfElements, $array2Name);

It has four parameters.

  1. $arrayName is a mandatory parameter. It is the array whose elements will be deleted.
  2. $startingIndex is the index of the element we wish to delete.
  3. $numOfElements is the number of elements we want to delete from the starting index.
  4. $array2Name is an array of elements we want to add.

$numOfElements and $array2Name are optional.

<?php
//Declare the array
$flowers = array(
                "Rose",
                "Lili",
                "Jasmine",
                "Hibiscus",
                "Tulip",
                "Sun Flower",
                "Daffodil",
                "Daisy");

array_splice($flowers, 4, 3);
echo "The array is:\n";
print_r($flowers);
?>

The array $flowers is passed as a parameter to this function along with the starting index 4 and the number of elements we want to delete-3. In this way, we can delete multiple elements from an array.

Output:

The array is:
Array
(
    [0] => Rose
    [1] => Lili
    [2] => Jasmine
    [3] => Hibiscus
    [4] => Daisy
)
Note

Different from unset, array_splice function will automatically reindex the keys.

Daisy has the new index as 4 but not the original index - 7, after we delete three elements before it.

Using array_diff() function to delete an element from an array in PHP

The built-in function array_diff() finds the difference between two or more arrays. It can be used to delete multiple values from an array without effecting their indexes. The correct syntax to use this function is as follows

array_diff($array1, $array2, $array3, ... , $arrayN);

It takes N number of arrays as the parameters. It compares the first array with all other arrays and returns an array that contains all the elements of the first array that are not present in other arrays.

<?php
//Declare the array
$flowers = array(
                "Rose",
                "Lili",
                "Jasmine",
                "Hibiscus",
                "Tulip",
                "Sun Flower",
                "Daffodil",
                "Daisy");

$flowers = array_diff($flowers, array("Rose","Lili"));
echo "The array is:\n";
print_r($flowers);
?>

Here, the first array that we have passed is $flowers and the second array contains the elements that we want to delete from $flowers. This function does not change the indexes of the elements of the array.

Output:

The array is:
Array
(
    [2] => Jasmine
    [3] => Hibiscus
    [4] => Tulip
    [5] => Sun Flower
    [6] => Daffodil
    [7] => Daisy
)

Related Article - PHP Array

  • How to Convert an Array to a String in PHP
  • How to Echo or Print an Array in PHP
  • comments powered by Disqus