+

Program for removing empty array elements in PHP

Method 1:using the array_filter() function . This is achieved using the array_filter() function. It also removes false values ​​when declared using a callback function, however, if no callback function is specified, all array values ​​that are FALSE will be removed, such as an empty string or NULL.Example :  
// Declare the array and the stored value of the array $array = array ( "engineer" , 11, `` , null, 12, "for" , 1997, false, "engineer" );  
// Function to remove empty elements // from array $filtered_array = array_filter ( $array ) ;  
// Show filtered array var_dump ( $filtered_array ); ?>
Exit:
array (6 ) {[0] = > string (5) "engineer" [1] = > int (11) [4] = > int (12) [5] = > string (3) "for" [6] = > int (1997) [8] = > string (5) "engineer"} 
Method 2.Using the function unset() . Another approach is to remove empty elements from the array using the empty() function along with the unset() function. The empty() function is used to check if an element is empty or not.Example :  
// Declare the array and the stored value of the array $array = array ( "engineer" , 11, ` ` , null, 12, "for" , 1997, false, "engineer" );  
// Loop to find empty elements and
// discard empty elements foreach ( $array as $key = > $value ) if ( empty ( $value )) unset ( $array [ $key ]);  
// Show array elements foreach ( $array as $key = > $value ) echo ( $array [ $key ]. "< br >" ); ?>
Exit:
engineer 
11
12 for
1997 on engineer
Get Solution for free from DataCamp guru