How to Delete an element from an array? explain with example

One of the most common tasks in PHP is deleting an element from an array. 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 the unset() Function

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

unset($variableName);

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

Example:-

<?php
   //Declare the array
   $nicname = array(
                "Vijay",
                "Amit",
                "Rahul",
                "Dharmendra",
                "Roshan");

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

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

Output:

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

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

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.
<?php
//Declare the array
$nicname = array(
                "Vijay",
                "Amit",
                "Dharmu",
                "Roshan",
                "Dilip",
                "Rahul",
                "Dhyanchand",
                "Shohan");

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

The array $nicname 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:-

Different from unsetarray_splice the 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.

Use 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 affecting their indexes.

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
$nicname = array(
                "Vijay",
                "Amit",
                "Dharmu",
                "Roshan",
                "Rahul",
                "Devanand",
                "Ramayan",
                "Dillyan");

$nicname = array_diff($nicname, array("Vijay","Amit"));
echo "The array is:\n";
print_r($nicname);
?>

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

Output:-

Related Posts

PHP – strip_tags() Replace with Spaces Example

We are going to learn php strip tags with spaces. You will learn how to replace php Strip_tags with space. In this article, we will replace php…

How to Get Duplicate Values from Array in PHP?

We will learn php array get duplicate values. we will help you to give an example of how to get duplicate values from array in php. you…

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

How Can I Convert a String in PHP Into an Array?

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x