Home > Backend Development > PHP Tutorial > Use PHP array_unique() function to remove duplicate arrays

Use PHP array_unique() function to remove duplicate arrays

WBOY
Release: 2023-06-27 12:40:02
Original
1378 people have browsed it

When developing web applications, it is often necessary to deduplicate arrays. PHP provides a very convenient array_unique() function to handle this situation. The array_unique() function can remove duplicate elements from an array and return a new array.

The basic syntax of the array_unique() function is as follows:

array_unique(array $array, int $sort_flags = SORT_STRING): array
Copy after login

Among them, the $array parameter specifies the array to remove duplicate elements; $sort_flags is an optional parameter, specifying the sorting rule, you can use SORT_NUMERIC, SORT_STRING or SORT_LOCALE_STRING means sorting by numerical value, by string and by localized string respectively. If $sort_flags is not specified, the function defaults to the SORT_STRING rule.

Now, let’s look at an example that demonstrates how to use the array_unique() function for array deduplication:

<?php
//定义一个包含重复元素的数组
$fruits = array("apple", "banana", "orange", "apple", "pear", "banana");

//使用array_unique()函数去重
$unique_fruits = array_unique($fruits);

//打印去重后的数组
print_r($unique_fruits);
?>
Copy after login

The above code will output the following results:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [4] => pear
)
Copy after login

You can see , there are only four elements left in the $unique_fruits array, and the duplicate "apple" and "banana" have been deleted.

It should be noted that the array_unique() function will re-index the key values ​​of the array. This means that after removing duplicate elements, the keys in the $unique_fruits array will be renumbered starting from 0.

If you want to keep the last copy of the duplicate element in the original array, you can traverse the array in reverse order, and then use the unset() function to delete the previous copy of the duplicate element. The specific code is as follows:

<?php
$names = array("Tom", "Jack", "Mary", "Tom", "John", "Mary");
$unique_names = array();
for($i = count($names) - 1; $i >= 0; $i--){
   if(!in_array($names[$i], $unique_names)){
      array_unshift($unique_names, $names[$i]);
   }
   else{
      unset($names[$i]);
   }
}
print_r($names); //输出数组中仅包含重复元素的最后一个副本
print_r($unique_names); //输出去重后的数组
?>
Copy after login

The above code will output the following two lines:

Array
(
    [2] => Mary
    [4] => John
)

Array
(
    [0] => Tom
    [1] => Jack
    [2] => Mary
    [4] => John
)
Copy after login

In this way, the $unique_names array stores the deduplicated array, and the $names array only retains duplicate elements. the last copy of.

The array_unique() function is one of the most practical functions in PHP. It can easily remove duplicate elements from an array, making the task of processing arrays easier.

The above is the detailed content of Use PHP array_unique() function to remove duplicate arrays. 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