目录

PHP array_unique() 函数

❮ PHP 数组参考

示例

从数组中删除重复值:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
亲自试一试 »

定义和用法

array_unique() 函数从数组中删除重复值。如果两个或多个数组值相同,则将保留第一个出现的值,而删除另一个出现的值。

笔记:返回的数组将保留第一的数组项的键类型。


语法

array_unique( array, sorttype)

参数值

Parameter Description
array Required. Specifying an array
sorttype Optional. Specifies how to compare the array elements/items. Possible values:
  • SORT_STRING - Default. Compare items as strings
  • SORT_REGULAR - Compare items normally (don't change types)
  • SORT_NUMERIC - Compare items numerically
  • SORT_LOCALE_STRING - Compare items as strings, based on current locale


技术细节

返回值: 返回过滤后的数组
PHP 版本: 4.0.1+
PHP 变更日志: PHP 7.2:如果排序类型是 SORT_STRING,这将返回一个新数组并添加唯一元素。
PHP 5.2.9:默认值排序类型已更改为 SORT_REGULAR。
PHP 5.2.1:默认值排序类型已更改回 SORT_STRING。

❮ PHP 数组参考