How to remove identical values ​​from php array (three methods)

PHPz
Release: 2023-04-18 10:39:14
Original
1680 people have browsed it

It is very common to use arrays in PHP. An array is a data structure used to store multiple values. When processing large amounts of data, you may encounter situations where you need to remove duplicate data from an array. This article will introduce several common methods to achieve PHP array deduplication.

1. Use the array_unique() function

PHP provides a built-in function array_unique() to remove duplicate elements from the array. This function returns a new array containing the only element in the original array.

Sample code:

$arr = array('a', 'b', 'b', 'c', 'c', 'c');
$arr = array_unique($arr);
print_r($arr);
Copy after login

Output result:

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

2. Use the loop method to remove duplicates

Use a for loop to traverse the array, if the same is found element, use the unset() function to delete the element from the array. It should be noted that after deleting elements, you need to rearrange the indexes in the array. You can use the array_values() function to rearrange the array indexes.

Sample code:

$arr = array('a', 'b', 'b', 'c', 'c', 'c');
$n = count($arr);
for($i = 0; $i < $n; $i++) {
    for($j = $i + 1; $j < $n; $j++) {
        if($arr[$i] == $arr[$j]) {
            unset($arr[$j]);
        }
    }
}
$arr = array_values($arr);
print_r($arr);
Copy after login

Output result:

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

3. Use array_flip() and array_keys() functions to remove duplicates

This method is very simple , you only need to use the original array as a parameter of the array_flip() function, and then use the array_keys() function to return all the key names in an array to get an array without repeated elements.

Sample code:

$arr = array('a', 'b', 'b', 'c', 'c', 'c');
$arr = array_flip($arr);
$arr = array_keys($arr);
print_r($arr);
Copy after login

Output result:

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

Summary:

There are many ways to remove duplicate elements from an array in PHP, such as using recursion Method, use array_filter() function, etc. However, the above three methods are relatively common methods, and you can choose different methods to operate arrays according to the actual situation. It should be noted that when using the unset() function to delete elements, the array index must be reset to ensure the correctness of the array.

The above is the detailed content of How to remove identical values ​​from php array (three methods). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!