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:
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
1 2 3 4 5 |
<?php $myarr = array('Apple', 'Apricots', 'Avocado', 'Banana', 'Mango', 'Orange' ); unset( $myarr[4] ); // unset 4th element print_r($myarr); ?> |
[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:
1 2 3 4 |
// pass $myarr to array_values $myarr2 = array_values($myarr); // display array returned by array_values print_r($myarr2); |
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:
1 2 3 4 5 6 7 8 9 |
<?php $student = array( 'studentname' => 'David', 'age' => 24, 'classmate' => array('John', 'Puspendra', 'Kamal', 'Ravindra') ); unset( $student['age'] ); print_r($student); ?> |
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:
1 2 3 4 5 6 7 |
<?php $myarr = array('Apple', 'Apricots', 'Avocado', 'Banana', 'Mango', 'Orange' ); $element = array_shift($myarr); var_dump($element); print_r($myarr);// view modified $myarr echo count($myarr); ?> |
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:
1 2 3 4 5 6 7 8 9 10 |
<?php $myarr = array('Apple', 'Apricots', 'Avocado', 'Banana', 'Mango', 'Orange' ); // pass $myarr to array_pop $element = array_pop($myarr); // inspect array_pop return value var_dump($element); // view modified $myarr print_r($myarr); echo count($myarr); ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $student = array( 'studentname' => 'David', 'Class' => 12, 'classmate' => array('John', 'Puspendra', 'Kamal', 'Ravindra') ); // pass $student to array_pop $element = array_pop($student); // inspect array_pop return value var_dump($element); // view modified $student print_r($student); ?> |
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):
1 2 3 4 5 6 7 8 |
<?php $myarr = array('John', 'Puspendra', 'Kamal', 'Ravindra', 'Suresh', 'Ramesh', 'Pinki', 'Aditya'); $r = array_splice($myarr, 4, 3); // view array returned by array_splice (array of removed elements) print_r($r); // view modified $myarr print_r($myarr); ?> |
[0] => Suresh
[1] => Ramesh
[2] => Pinki
)
Array (
[0] => John
[1] => Puspendra
[2] => Kamal
[3] => Ravindra
[4] => Aditya
)
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!