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

php remove duplicate data in array

WBOY
Release: 2023-05-19 17:02:38
Original
470 people have browsed it

PHP is an open source scripting language widely used in the field of web development. It can handle various data types and data structures, such as strings, arrays, objects, etc. In web development, we often need to deduplicate data, which can improve data processing efficiency. This article will introduce a method of removing duplicate data in an array in PHP.

Method 1: Use the array_unique() function

The array_unique() function in php can be used to remove duplicate data in the array. This function accepts an array as argument and returns a new array that does not contain duplicate values.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$arr_unique = array_unique($arr);
print_r($arr_unique);
Copy after login

Output result:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
)
Copy after login
Copy after login
Copy after login

Method 2: Use loop traversal

In addition to using the array_unique() function, we can also pass Deduplication is achieved through loop traversal. The specific steps are as follows:

  1. Define a new array to store unique data.
  2. Traverse the original array, compare each element with each element in the new array one by one, skip if it is repeated, add the element to the new array if it is not repeated.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$new_arr = array();
foreach ($arr as $value) {
    if (!in_array($value, $new_arr)) {
        $new_arr[] = $value;
    }
}
print_r($new_arr);
Copy after login

Output result:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
)
Copy after login
Copy after login
Copy after login

Method 3: Use array_flip() and array_keys() functions

in php The array_flip() function can be used to swap keys and values ​​in an array. By exchanging the key values ​​of the original array and then using the array_keys() function to obtain the keys, the deduplicated array can be obtained.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$new_arr = array_keys(array_flip($arr));
print_r($new_arr);
Copy after login

Output result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
Copy after login

Method 4: Use the array_filter() function

The array_filter() function in php can be used for filtering elements in the array. We can deduplicate the original array by using this function combined with an anonymous function.

Sample code:

$arr = array(1, 2, 2, 3, 4, 4);
$new_arr = array_filter($arr, function ($value) {
    static $data = [];
    if (in_array($value, $data)) {
        return false;
    } else {
        $data[] = $value;
        return true;
    }
});
print_r($new_arr);
Copy after login

Output result:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
)
Copy after login
Copy after login
Copy after login

Summary:

There are many ways to remove duplicate data in an array in php, as described above Methods are just a few of them. We can choose appropriate deduplication methods based on different needs and data structures to improve the efficiency and stability of the program.

The above is the detailed content of php remove duplicate data in 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