Home  >  Article  >  Backend Development  >  PHP array function array_unique() removes duplicate values ​​from the array

PHP array function array_unique() removes duplicate values ​​from the array

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 10:38:215420browse

This time I will bring you php arrayfunction array_unique() to remove duplicate values ​​in the array, php array function array_unique() to remove duplicate values ​​from the array What are the precautions? The following is a practical case, let’s take a look.

array_unique() function removes duplicate values ​​from an 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.

array_unique() Definition and Usage
array_unique() function removes duplicate values ​​from an 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 name.
This does not mean that the first occurrence of the same value in the unsorted array will be retained.

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

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

Output:

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

array_key_exists() function detailed steps to search array key names

Summary of how to use php array search function

The above is the detailed content of PHP array function array_unique() removes duplicate values ​​from the array. For more information, please follow other related articles on the PHP Chinese website!

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