Home >Backend Development >PHP Problem >How to remove duplicate values ​​from a field value in PHP

How to remove duplicate values ​​from a field value in PHP

WBOY
WBOYOriginal
2022-01-10 16:04:382066browse

Method: 1. Use the explode() function to split the specified field into an array; 2. Use the "array_unique" function to remove duplicate values ​​in the array; 3. Use the implode() function to remove duplicate values ​​from the array. Just combine it into a string.

How to remove duplicate values ​​from a field value in PHP

The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

php a field value How to remove duplicate values

explode(',',$data) splits the string into an array

array_unique() removes duplicate values ​​from the array

implode(',',$data))) Use, to split and combine the array into a string

The example is as follows:

$data = "xiaomi,xiaohong,xiaogang,xiaomi,xiaohu";
$data = implode(',',array_unique(explode(',',$data))); 
echo $data;

//The result is $data="xiaomi, xiaohong,xiaogang,xiaohu";

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

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