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

How to remove duplicate values ​​in php

藏色散人
Release: 2023-03-01 21:18:02
Original
2674 people have browsed it

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)
Copy after login

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));
?>
Copy after login

Output:

Array ( [a] => Cat [b] => Dog )
Copy after login

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!

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