Use unset() function to remove array elements in a foreach loop. The unset() function is an inbuilt function in PHP which is used to unset a specified variable. The behavior of this function depends on different things. If the function is called from inside of any user defined function then it unsets the value associated with the variables inside it, leaving the value which is initialized outside it.
unset( $variable )
Return Value: This function does not returns any value.
Below examples use unset() function to remove an array element in foreach loop.
Array (  => GeeksForGeeks_1  => GeeksForGeeks_2  => GeeksForGeeks_3 ) Array (  => GeeksForGeeks_1  => GeeksForGeeks_2 )
Array (  => Array (  => 1 )  => Array (  => 10 )  => Array (  => 100 ) ) Array (  => Array (  => 1 )  => Array (  => 10 ) )
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- Iterate associative array using foreach loop in PHP
- Determine the first and last iteration in a foreach loop in PHP?
- C# | Using foreach loop in arrays
- PHP | foreach Loop
- How to find the index of foreach loop in PHP ?
- How to use for and foreach loop in Golang?
- Java Program to Iterate Over Arrays Using for and foreach Loop
- ES6 | Array forEach() Method
- TypeScript | Array forEach() Method
- Performance of for vs foreach in PHP
- AngularJS | angular.forEach() Function
- Node.js | forEach() function
- HashMap forEach(BiConsumer) method in Java with Examples
- HTML DOM NodeList.forEach() Method
- Lodash _.forEach() Method
- How to remove parent element except its child element using jQuery ?
- PHP | Separate odd and even elements from array without using loop
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.