Be careful with the main answer. Show with
and calling the function
it returns
instead of
It is because unset does not re-index the array. It reindexes. (if we need it) View Discussion Improve Article Save Article View Discussion Improve Article Save Article 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. Syntax: unset( $variable ) Return Value: This function does not returns any value. Below examples use unset() function to remove an array element in foreach loop. Example 1:
Output: Array ( [0] => GeeksForGeeks_1 [1] => GeeksForGeeks_2 [2] => GeeksForGeeks_3 ) Array ( [0] => GeeksForGeeks_1 [1] => GeeksForGeeks_2 ) Example 2:
Output: Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [4] => 10 ) [2] => Array ( [6] => 100 ) ) Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [4] => 10 ) ) In this post, we will see how to remove a specific element from an array by its value in PHP. 1. Using array_search() functionIf the array contains only a single element with the specified value, you can use the array_search() function with
unset() function to remove it from an array. The
2. Using foreach constructIf your array can contain multiple elements with the specified value, you can use a simple foreach loop to iterate over the array, and unset all matching values.
That’s all about removing a specific element from an array in PHP.
Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂 How do I remove an object from an array in foreach?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.
How to remove 1 element from array in PHP?In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically. Function Used: unset(): This function unsets a given variable.
Can we remove an element by using foreach loop?The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove . Therefore, the for-each loop is not usable for filtering.
How do you remove an object from an array?There are different methods and techniques you can use to remove elements from JavaScript arrays:. pop - Removes from the End of an Array.. shift - Removes from the beginning of an Array.. splice - removes from a specific Array index.. filter - allows you to programatically remove elements from an Array.. |