Home > Backend Development > PHP Tutorial > PHP array function sequence array_unique() - remove duplicate element values ​​​​in the array

PHP array function sequence array_unique() - remove duplicate element values ​​​​in the array

高洛峰
Release: 2023-03-04 20:24:01
Original
1487 people have browsed it

array_unique() Definition and Usage
array_unique() function removes duplicate values ​​from the array and returns the resulting array.

When the values ​​of several array elements are equal, only the first element is retained and the other elements are deleted.

The key names in the returned array remain unchanged.

Syntax
array_unique(array)
Parameter Description
array Required. Specifies the input array.

Description
array_unique() first sorts the values ​​as strings, then only retains the first encountered key name for each value, and then ignores all subsequent key names. This does not mean that the first occurrence of the same value in an unsorted array will be preserved.

Tips and Notes
Note: The returned array will retain the key type of the first array element.

Example

<?php 
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat"); 
print_r(array_unique($a)); 
?>
Copy after login

Output:

Array ([a] => Cat [b] => Dog)

More php array functions Sequence array_unique() - Remove duplicate element values ​​​​in an array. For related articles, please pay attention to the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template