- 相關推薦
分享php數(shù)組去除空值函數(shù)
對于一個一維的php數(shù)組,如何清除其中值為空的元素呢?直接的辦法是foreach循環(huán)一下,一個個判斷排除。不過這個方法還是略顯復雜,下面分享一下今天看到的一個方法,非常簡潔
復制代碼 代碼如下:
/**
* 方法庫-數(shù)組去除空值
* @param string $num 數(shù)值
* @return strin
*/
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;
}
}
}
}
是不是非常實用的函數(shù)呢,希望大家能夠喜歡。
【分享php數(shù)組去除空值函數(shù)】相關文章:
php數(shù)組函數(shù)序列之a(chǎn)rray-combine() - 數(shù)組合并函數(shù)的代碼08-25
如何獲取PHP數(shù)組的鍵與值呢10-25
PHP的壓縮函數(shù)06-21
淺析php函數(shù)的實例06-08
簡單介紹php構造函數(shù)用法08-31
PHP中函數(shù)的使用說明09-01