php数组去除空值函数分享
Jun 06, 2016 pm 08:11 PM本文给大家分享一个使用php制作数组去除空值函数,非常实用,推荐给大家,希望大家能够喜欢。
对于一个一维的php数组,如何清除其中值为空的元素呢?直接的办法是foreach循环一下,一个个判断排除。不过这个方法还是略显复杂,下面分享一下今天看到的一个方法,,非常简洁
复制代码 代码如下:
/**
* 方法库-数组去除空值
* @param string $num 数值
* @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;
}
}
}
}
是不是非常实用的函数呢,希望大家能够喜欢。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
