Home  >  Article  >  php教程  >  给数组去除重复数据

给数组去除重复数据

PHP中文网
PHP中文网Original
2016-05-25 17:15:26956browse

给数组排重

与array_unique函数的区别:它要求val是字符串,而这个可以是数组/对象

/**
		 * 给数组排重
		 * 与array_unique函数的区别:它要求val是字符串,而这个可以是数组/对象
		 *
		 * @param unknown_type $arr 要排重的数组
		 * @param unknown_type $reserveKey 是否保留原来的Key
		 * @return unknown
		 */
		static 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;
		}
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