Home >Backend Development >PHP Problem >How to delete blank elements in an array in php
The way PHP deletes blank elements in an array is to first judge the array. Then iterate through the elements in the array one by one to determine whether the element is null. Finally, use the unset function to delete blank elements.
The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.
The unset function can release the given variable. By using this function, we can delete blank elements from the array without any worries. Let's take a look at how to delete blank elements in an array.
Implementation code:
/* *$arr = array('', 'test', ' '); *dump($arr);输出结果中将只有 'test' */ public function removeEmpty($arr, $trim = TRUE) { foreach ($arr as $key => $value){ if (is_array($value)){ self::removeEmpty($arr[$key]); } else{ $value = trim($value); if ($value == ''){ unset($arr[$key]); } elseif ($trim){ $arr[$key] = $value; } } } }
You can save the above code locally and see the effect by running it locally.
Related video tutorial sharing: php video tutorial
The above is the detailed content of How to delete blank elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!