Home > php教程 > PHP源码 > body text

给数组去除重复数据

PHP中文网
Release: 2016-05-25 17:15:26
Original
998 people have browsed it

给数组排重

与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;
		}
Copy after login
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template