You can simply use the PHP array_filter() function to remove or filter empty values from an array. This function typically filters the values of an array using a callback function.
However, if no callback function is specified, all empty entries of array will be removed, such as "" (an empty string), 0 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string), NULL, FALSE and array() (an empty array). Let's try out an example to understand how it actually works:
In the above example the values 0 and "0" are also removed from the array. If you want to keep them, you can define a callback function as shown in the following example:
The callback function myFilter() is called for each element of the array. If myFilter() returns TRUE, then that element will be appended to the result array, otherwise not.
Use the PHP
array_filter()functionYou can simply use the PHP
array_filter()function to remove or filter empty values from an array. This function typically filters the values of an array using a callback function.However, if no callback function is specified, all empty entries of array will be removed, such as
""(an empty string),0(0 as an integer),0.0(0 as a float),"0"(0 as a string),NULL,FALSEandarray()(an empty array). Let's try out an example to understand how it actually works:In the above example the values 0 and "0" are also removed from the array. If you want to keep them, you can define a callback function as shown in the following example:
The callback function myFilter() is called for each element of the array. If myFilter() returns TRUE, then that element will be appended to the result array, otherwise not.
need an explanation for this answer? contact us directly to get an explanation for this answer