How to remove duplicate values ​​from php array

PHPz
Release: 2023-04-26 10:47:07
Original
545 people have browsed it

PHP arrays are containers used to store the same type of data. However, sometimes we accidentally add duplicate values ​​​​in the array, and these duplicate values ​​​​will bring us unnecessary trouble. In this article, we will introduce how to use PHP language to remove duplicate values ​​​​from an array.

1. Use the array_unique() function in PHP to remove duplicate values ​​

array_unique() is a function in PHP that is used to remove duplicate values ​​from an array. Using this function can easily remove duplicate values ​​from the array. The code is as follows:

$arr = array("a", "b", "c", "d", "b", "c"); //定义一个含有重复值的数组
$arr = array_unique($arr); //将数组中的重复值去掉
print_r($arr); //输出去重后的数组
Copy after login

In the above code, the $arr array contains duplicate values ​​"b" and "c", and then we use array_unique () function removes these duplicate values, and finally outputs the deduplicated array, that is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Copy after login
Copy after login

2. Use the for loop and if statement in PHP to remove duplicate values ​​

In addition to using array_unique () function, we can also use for loops and if statements to traverse the array and remove duplicate values. The code is as follows:

$arr = array("a", "b", "c", "d", "b", "c"); //定义一个含有重复值的数组
$new_arr = array(); //定义一个新的空数组
for($i = 0; $i < count($arr); $i++) {
    if(!in_array($arr[$i], $new_arr)) { //判断$new_arr数组中是否已经存在该元素
        array_push($new_arr, $arr[$i]); //如果不存在,则将该元素加入$new_arr数组中 
    }
}
print_r($new_arr); //输出去重后的数组
Copy after login

In the above code, we first define an array containing duplicate values array $arr, and then defines a new empty array $new_arr. Next, we use a for loop and if statement to traverse the array $arr. If an element does not exist in $new_arr, then add the element to the new array, and finally output the deduplicated $new_arr array, that is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Copy after login

3. Use the array_flip() function and array_keys() function in PHP to remove duplicate values

Another clever way is to use the array_flip() function and array_keys() function in PHP to remove duplicate values. value, the specific code is as follows:

$arr = array("a", "b", "c", "d", "b", "c"); //定义一个含有重复值的数组
$arr = array_flip($arr); //使用array_flip()函数将数组中的值和键进行互换
$arr = array_keys($arr); //使用array_keys()函数提取数组中的键
print_r($arr); //输出去重后的数组
Copy after login

In the above code, we first use the array_flip() function to exchange the key value of the array $arr containing repeated values, and then use the array_keys() function to extract the keys in the array Key, you can get the deduplicated array, and finally output it, that is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Copy after login
Copy after login

Summary:

PHP array is a very commonly used data type, but when using the array, there will be Encountered a problem with duplicate values. This article introduces three methods for removing duplicate values ​​​​in arrays. Different methods can be selected according to the specific actual needs. The most convenient one is the array_unique() function, and the most clever one is to use the array_flip() function and array_keys() function. These methods can help us handle duplicate values ​​​​that exist in PHP arrays more effectively and improve the efficiency of the program.

The above is the detailed content of How to remove duplicate values ​​from php array. For more information, please follow other related articles on the PHP Chinese website!

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