Program to remove empty array elements in PHP Last Updated : 20 Sep, 2018 Comments Improve Suggest changes Like Article Like Report Given an array containing elements. The task is to remove empty elements from the array such as an empty string or a NULL element. Method 1: Using array_filter() Function. It is achieved by using array_filter() function. It also removes false values when declared using a callback function, however, if no callback function is specified, all the values of the array which are equal to FALSE will be removed, such as an empty string or a NULL value. Example: php <?php // Declare array and stored array value $array = array("geeks", 11, '', null, 12, "for", 1997, false, "geeks"); // Function to remove empty elements // from array $filtered_array = array_filter($array); // Display the filtered array var_dump($filtered_array); ?> Output: array(6) { [0]=> string(5) "geeks" [1]=> int(11) [4]=> int(12) [5]=> string(3) "for" [6]=> int(1997) [8]=> string(5) "geeks" } Method 2: Using unset() Function. Another approach is to remove empty elements from array is using empty() function along with the unset() function. The empty() function is used to check if an element is empty or not. Example: php <?php // Declare array and stored array value $array = array("geeks", 11, '', null, 12, "for", 1997, false, "geeks"); // Loop to find empty elements and // unset the empty elements foreach($array as $key => $value) if(empty($value)) unset($array[$key]); // Display the array elements foreach($array as $key => $value) echo ($array[$key] . "<br>"); ?> Output: geeks1112for1997geeks Comment More infoAdvertise with us Next Article Program to remove empty array elements in PHP S sarthak_ishu11 Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to Remove First Element from an Array in PHP? Given an Array, the task is to remove the first element from the array in PHP. There are three methods to remove the first element from an array, these are - using array_shift(), array_slice(), and unset() functions. Let's explore each method with detailed explanations and code examples.Table of Con 3 min read How to Remove Last Element from an Array in PHP ? Removing the last element from an array is a common operation in PHP. There are several approaches to achieve this. In this article, we will explore different methods to remove the last element from an array.Table of ContentUsing array_pop() FunctionUsing array_slice() FunctionUsing unset() Function 3 min read How to Remove Multiple Elements from an Array in PHP? Given an array containing some elements, the task is to remove some elements from the array in PHP. Below are the approaches to remove multiple elements from an array in PHP:Table of ContentUsing array_diff() FunctionUsing array_filter() FunctionUsing a Loop and unset() FunctionUsing array_diff_key( 4 min read Remove an Elements From End of an Array in PHP Given an array "myArray", the task is to remove an element from its end. This operation is important in scenarios involving stacks, queues, and web development. In PHP, there are several approaches to achieve this One common method is using the array_pop() function, which removes the last element fr 3 min read Remove Specific Element from an Array in PHP Given an Array containing some elements, the task is to remove specific elements from the array in PHP. Below are the approaches to remove specific elements from an array in PHP: Table of Content Using unset() FunctionUsing array_diff() FunctionUsing array_filter() FunctionUsing array_splice() Funct 3 min read Like