Home  >  Article  >  Backend Development  >  How to remove duplicates from an array in php

How to remove duplicates from an array in php

王林
王林Original
2021-05-13 15:44:242054browse

The way PHP removes duplicates from an array is to use the array_unique() function, such as [array_unique(array)]. The array_unique function is a function specifically used to remove duplicate values ​​from an array.

How to remove duplicates from an array in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

In PHP we can use the array_unique function to remove duplicate items in the array. Let’s introduce this function first.

array_unique() function is used to remove duplicate values ​​in 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.

Grammar:

array_unique(array)

A small example:

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

Running results:

Array ( [a] => red [b] => green )

Free learning video sharing: Introduction to Programming

The above is the detailed content of How to remove duplicates from an array 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