Latest web development tutorials

PHP array_unique () function

PHP Array Reference Complete PHP Array Reference

Examples

Remove duplicate values ​​in the array:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>

Running instance »

Definition and Usage

array_unique () function is used to remove duplicate values ​​in the array. If two or more of the same array of values, retaining only the first value, other values ​​are removed.

Note: The array will be retained to keep the firstitem in the array type keys.


grammar

array_unique( array )

参数 描述
array 必需。规定数组。
sortingtype 可选。规定排序类型。可能的值:
  • SORT_STRING - 默认。把每一项作为字符串来处理。
  • SORT_REGULAR - 把每一项按常规顺序排列(Standard ASCII,不改变类型)。
  • SORT_NUMERIC - 把每一项作为数字来处理。
  • SORT_LOCALE_STRING - 把每一项作为字符串来处理,基于当前区域设置(可通过 setlocale() 进行更改)。

technical details

return value: Returns an array filtered.
PHP version: 4.0.1+
Update log: In PHP 5.2.10, the default value is changed back to sortingtype SORT_STRING.

In PHP 5.2.9, the default value sortingtype changed SORT_REGULAR. Prior to this version, sortingtype defaults SORT_STRING.


PHP Array Reference Complete PHP Array Reference