Home >Backend Development >PHP Problem >How to remove duplicate data from array in php

How to remove duplicate data from array in php

王林
王林Original
2020-08-11 14:21:122678browse

How to delete duplicate data in an array in php: You can use the php built-in function array_unique() to achieve this. The array_unique() function is used to remove duplicate values ​​from the array and return the filtered array. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed.

How to remove duplicate data from array in php

array_unique() function is used to remove duplicate values ​​from an array and return the filtered array.

(Recommended tutorial: php graphic tutorial)

If two or more array values ​​are the same, only the first value is retained, and the other values ​​are moved remove.

Syntax:

array_unique(array)

Parameters:

  • array Required. Specifies array

  • sortingtype Optional. Specify the sorting type

(Video tutorial recommendation: php video tutorial)

Example:

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

Output result:

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

The above is the detailed content of How to remove duplicate data from 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