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

Function to remove duplicate values ​​from array_PHP tutorial

PHP中文网
Release: 2016-07-13 10:30:57
Original
1227 people have browsed it


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;
}
Copy after login

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 (m.sbmmt.com)!

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...


Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template