Quick PHP tip: It is common to want to explode a string based on some delimiting character, then iterate over the array. Most of the time you don’t care about the empty items in the array. Suppose, for example, you have a dynamically constructed string of id numbers from a database or something and you want to iterate over them and do something. Here is a quick code sample to remove any entry that evaluates as false in the array.
$csvIdString = '1,2,3,,5,6,,8,'; $ids = array_filter(explode(',', $csvIdString)); print_r($ids); /* Result: Array (  => 1  => 2  => 3  => 5  => 6  => 8 ) */
Suppose you want to take a CSV string that might have some empty slots and clean it up.
$csvString = ',1,2,3,,5,6,,8,'; $string = implode(',', array_filter(explode(',', $csvString))); echo "String: $string"; /* Result: String: 1,2,3,5,6,8 */
Again, note that any array item that returns false will be removed. This includes 0, ” (empty strings), null, ‘false’, etc. The array_filter function can take a 2nd parameter for a callback function. This will let you do much more powerful decisioning on which items to remove from the array. Just have your callback return false for values you want to have removed.