Home  >  Article  >  Backend Development  >  Function to remove duplicate values ​​from array_PHP tutorial

Function to remove duplicate values ​​from array_PHP tutorial

PHP中文网
PHP中文网Original
2016-07-13 10:30:571167browse


php’s function to remove duplicate values ​​from an array can also be understood as removing duplicate values ​​from an array.

/**
* 给数组排重
* 与array_unique函数的区别:它要求val是字符串,而这个可以是数组/对象
*
* @param $arr 要排重的数组
* @param $reserveKey 是否保留原来的Key
* @return array
*/
function m_ArrayUnique($arr,$reserveKey=false){
	if(is_array($arr) && !empty($arr)){
		foreach($arr as $key=>$value){
			$tmpArr[$key]=serialize($value).'';
		}
		$tmpArr=array_unique($tmpArr);
		$arr=array();
		foreach($tmpArr as $key=>$value){
			if($reserveKey){
				$arr[$key]=unserialize($value);
			}else{
				$arr[]=unserialize($value);
			}
		}
	}
	return $arr;
}

For how to remove duplicate values ​​from a two-dimensional array, you can refer to: Summary of methods for removing duplicate values ​​from a two-dimensional array in PHP

The above is the function to remove duplicate values ​​from an array_PHP Tutorial For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

http://www.bkjia.com/PHPjc/764186.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/764186.htmlTechArticlephp's function to remove duplicate values ​​​​from an array can also be understood To remove duplicate values ​​from an array. /*** Rearrange the array* The difference with the array_unique function: it requires val to be a string, and this can be...


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