Home  >  Article  >  Backend Development  >  PHP array removal null value function sharing, PHP array function sharing_PHP tutorial

PHP array removal null value function sharing, PHP array function sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:19844browse

Sharing of php array function to remove null values, sharing of php array function

For a one-dimensional php array, how to clear the elements with empty values? The direct way is to loop through foreach and judge and exclude one by one. However, this method is still a bit complicated. Let me share a method I saw today. It is very simple

Copy code The code is as follows:

/**
* Method library-remove null values ​​from array
* @param string $num numerical value
* @return string
​*/
public function array_remove_empty(&$arr, $trim = true) {
If (!is_array($arr)) return false;
foreach($arr as $key => $value){
If (is_array($value)) {
                self::array_remove_empty($arr[$key]);
         } else {
$value = ($trim == true) ? trim($value) : $value;
                if ($value == "") {
                      unset($arr[$key]);
              } else {
$arr[$key] = $value;
            }
}
}
}

Isn’t this a very practical function? I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/951635.htmlTechArticlePHP array removal null value function sharing, php array function sharing For a one-dimensional php array, how to clear the value What about empty elements? The direct way is to loop through foreach, one by one...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn