Home >Backend Development >PHP Tutorial >PHP function introduction—array_unique(): remove duplicate elements from an array

PHP function introduction—array_unique(): remove duplicate elements from an array

王林
王林Original
2023-07-25 12:37:161299browse

PHP function introduction—array_unique(): Remove duplicate elements from an array

In PHP, we often encounter situations where we need to remove duplicate elements from an array. At this time, you can use PHP's built-in array_unique() function to achieve this. This article will introduce the usage of array_unique() function in detail and give code examples for readers' reference.

The syntax of the array_unique() function is as follows:
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Among them, $array represents the array to be processed, $sort_flags is an optional parameter that specifies how to sort the array.

The following is a simple example that demonstrates how to use the array_unique() function to remove duplicate elements in an array:

<?php
// 定义一个数组包含重复元素
$array = array(1, 2, 2, 3, 4, 4, 5);

// 使用array_unique()函数去除重复元素
$result = array_unique($array);

// 输出结果
print_r($result);
?>

The output of the above code is:

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
    [6] => 5
)

From the output As can be seen from the results, the array_unique() function successfully removes duplicate elements from the array and returns a new array. In the new array, each element appears only once.

It should be noted that the array_unique() function will retain the first appearing element and remove subsequent duplicate elements. Therefore, when using the array_unique() function, you should ensure that the elements in the array are arranged in the expected order.

In addition, it can only be used for one-dimensional arrays and cannot be applied to multi-dimensional arrays. If you want to remove duplicate elements from a multidimensional array, you need to resort to other methods, such as using recursive functions.

In addition to removing duplicate elements, the array_unique() function can also sort the array. The sorting method can be specified through the $sort_flags parameter. The value of $sort_flags can be the following constants:

  • SORT_REGULAR - Sort according to normal comparison rules
  • SORT_NUMERIC - Sort according to numerical size
  • SORT_STRING - Sort according to the comparison rules of strings

The following example demonstrates how to use the array_unique() function to sort an array:

<?php
// 定义一个数组包含重复元素
$array = array(4, 1, 3, 2, 2, 1, 4);

// 使用array_unique()函数对数组进行排序
$result = array_unique($array, SORT_NUMERIC);

// 输出结果
print_r($result);
?>

The output of the above code is:

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

As you can see from the output results, the arrays are sorted according to their numerical size.

To sum up, the array_unique() function is a practical PHP function that can quickly remove duplicate elements from an array. Arrays can also be sorted via the optional sort parameter. Readers can flexibly use this function according to their own needs. I hope this article can help readers better understand and use the array_unique() function.

The above is the detailed content of PHP function introduction—array_unique(): remove duplicate elements from an array. 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