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

How to remove duplicate values ​​from an array in php

王林
Release: 2023-03-02 10:48:02
Original
3148 people have browsed it

php method to remove duplicate values ​​​​in an array: This can be achieved through the array_unique() function. This function is used to remove duplicate values ​​in the array and return the filtered array. The syntax format is: [array_unique(array)].

How to remove duplicate values ​​from an array in php

Function introduction:

(Recommended tutorial: php tutorial)

array_unique() function 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.

Syntax:

array_unique(array)
Copy after login

Return value:

Return the filtered array.

Code implementation:

<?php
$input = array("a" => "green","", "red","b" => "green", "","blue", "red","c" => "witer","hello","witer");
//$result = array_unique($input); //去除重复元素
$result = a_array_unique($input);   //只留下单一元素
foreach($result as $aa)
{
echo $aa."<br />";
}
function multi_unique($array) {
   foreach ($array as $k=>$na)
       $new[$k] = serialize($na);
   $uniq = array_unique($new);
   foreach($uniq as $k=>$ser)
       $new1[$k] = unserialize($ser);
   return ($new1);
}

function a_array_unique($array)//写的比较好
{
   $out = array();
   foreach ($array as $key=>$value) {
       if (!in_array($value, $out))
{
           $out[$key] = $value;
       }
   }
   return $out;
} 
?>
Copy after login

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