首页 > 文章列表 > php中unset函数的使用

php中unset函数的使用

php unset
305 2022-08-06

说明

1、unset函数可以释放给定的变量。通过使用该函数,可以删除数组中的空白元素。

2、该函数接受索引并删除指定索引上存在的元素。

语法

void unset ( mixed $var [, mixed $... ] )

参数

$var: 要销毁的变量。

返回值

没有返回值。

实例

/*
 
 *$arr = array('', 'test', '   ');
 
 *dump($arr);输出结果中将只有 'test'
 
 *$arr = www.why114.com ;
 
 *$arr = www.92jzh.com ;
 
 */
 
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;
 
            }
 
        }
 
    }
 
}

推荐操作系统:windows7系统、PHP5.6、DELL G3电脑