Home  >  Article  >  Backend Development  >  php数组函数序列之array_unique()

php数组函数序列之array_unique()

WBOY
WBOYOriginal
2016-06-01 12:14:40700browse
array_unique() 定义和用法
array_unique() 函数移除数组中的重复的值,并返回结果数组。

当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除。

返回的数组中键名不变。

语法
array_unique(array)
参数 描述
array 必需。规定输入的数组。

说明
array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。

提示和注释
注释:被返回的数组将保持第一个数组元素的键类型。

例子
复制代码 代码如下:
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
print_r(array_unique($a));
?>

输出:

Array ( [a] => Cat [b] => Dog )
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