Remove From Array PHP | How to Delete an Element from an Array

Summary: In this artcile, we demonstrate and describe the various ways that you can remove elements from arrays in PHP. such as the unset, array_pop, and array_shift, functions. furthermore, the array_splice function in PHP, which can also be used to remove array elements, is discussed elsewhere.

Remove From Array PHP Overview:

This tutorial covers a few things regarding Remove From Array PHP:

TABLE OF CONTENT
1. Using PHP unset( ) Function
2. Using PHP array_shift Function
3. Using PHP array_pop Function
4. Using PHP array_splice Function

1. Using PHP unset( ) Function To Remove Individual Elements From an Array

If you want to delete an element from an array or want to remove individual elements from an array then you can simply use the PHP unset() function. The built-in function PHP unset() is used to delete the value stored in a variable. It’s only applicable to the local variables. It doesn’t reflect its behavior on global variables. so, with the use of this function, we can delete an element from an array. The following demonstrates using how to delete an element from the third element from an example array:

Program

Array (
[0] => Apple
[1] => Apricots
[2] => Avocado
[3] => Banana
[5] => Orange
)

As per the result, you can notice that the unset function leaves a gap in our numerically indexed array. so, we can use the PHP array_values function to return a re-indexed copy:

Array ( [0] => Apple [1] => Apricots [2] => Avocado [3] => Banana [4] => Mango [5] => Orange )

The PHP array_values function is re-indexed, as you can see in the above output result, the array passed to it isn’t affected. We can use again the PHP unset function to remove a key/value pair from an associative array, There are the following demonstrates:

Array ( [studentname] => David [classmate] => Array ( [0] => John [1] => Puspendra [2] => Kamal [3] => Ravindra ) )

2. Using PHP array_shift() Function To Removing The First Element From The Array

The PHP array_shift() function is Identical to PHP array_pop() except that instead of removing the last element, furthermore, it removes the first. The array_shift() function returns the removed element and reduces the length of the array passed to it by one. Let’s see the following examples:

Returned by array_shift:- string ‘Apple’ (length=5)
View modified $myarr:- Array ( [0] => Apricots [1] => Avocado [2] => Banana [3] => Mango [4] => Orange )
Resulting length of $myarr:- 5

3. Using PHP array_pop Function To Removes The Last Element From The Array

The PHP array_pop function removes the last element from the array passed to it. furthermore, it returns that removed element and reduces the length of the array by one. Let’s see the following examples:

String:- ‘Orange’ (length=6)
View modified $myarr:- Array ( [0] => Apple [1] => Apricots [2] => Avocado [3] => Banana [4] => Mango )
Total element: 5

The PHP array_pop function can be applied to associative arrays as well as numerically indexed ones, let’s see the given below program.

array (size=4)
0 => string ‘John’ (length=4)
1 => string ‘Puspendra’ (length=9)
2 => string ‘Kamal’ (length=5)
3 => string ‘Ravindra’ (length=8)
Array ( [studentname] => David [Class] => 12 )

4. Using PHP array_splice() Function To Remove Elements Anywhere in An Array.

The PHP array_splice function can be used also to remove elements anywhere in an array. The given below example demonstrates removing two elements from an array, starting from an offset of 4 (i.e., the 3th element):

Array (
[0] => Suresh
[1] => Ramesh
[2] => Pinki
)

Array (
[0] => John
[1] => Puspendra
[2] => Kamal
[3] => Ravindra
[4] => Aditya
)

.
Important Note: PHP’s array_splice Function is also used to Add Elements to an Array.
PHP’s array_splice Function is used to insert new elements anywhere.
We can also using a negative offset to remove elements from an array:

Conclusion: Remove From Array PHP

In this article, you have learned the various ways which you can used to remove elements from arrays in PHP. such as the unset, array_pop, and array_shift, functions. furthermore, also learned the array_splice function. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!