Home  >  Article  >  Backend Development  >  How to remove duplicate values ​​in php

How to remove duplicate values ​​in php

藏色散人
藏色散人Original
2020-07-03 10:47:452631browse

In PHP, you can use the "array_unique()" function to remove duplicate values. The function of this function is that when the values ​​​​of several array elements are equal, only the first element is retained, and the other elements are deleted. The syntax is "array_unique(array)".

How to remove duplicate values ​​in php

php remove duplicate values

array_unique() function definition and usage

## The #array_unique() function is used to remove duplicate values ​​from an array. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed.

Note: The retained array will retain the key type of the first array item.

Syntax

array_unique(array)

Parameters

array required. Specifies an array.

sortingtype Optional. Specifies the sorting type. Possible values:

SORT_STRING - Default. Treat each item as a string.

SORT_REGULAR - Sort each item in regular order (Standard ASCII, does not change type).

SORT_NUMERIC - Treat each item as a number.

SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).

Example:

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.

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

Output:

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

Recommended: "

PHP Tutorial"

The above is the detailed content of How to remove duplicate values ​​in php. 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