Home>Article>Backend Development> How to remove duplicate elements in PHP array

How to remove duplicate elements in PHP array

黄舟
黄舟 Original
2017-05-04 11:44:14 2220browse

Our last article talked about "How to delete the head, tail, and any element in a PHP array". In this article, we talk about deleting duplicate elements in the array through the array_unique() function.

array_unique() function sorts the values of the array elements as strings, and then only retains the first key name for each value and ignores all subsequent key names, which is to delete duplicate elements in the array,

The syntax format is as follows:

array arry_unique(array array)

The parameter array is the input array.

The following example uses the array_unique() function to delete duplicate elements in the array. The specific example code is as follows:

"; $result=array_unique($array_push);//删除数组中重复的元素 print_r($result); //输出删除后的数组 ?>

The output result is:

How to remove duplicate elements in PHP array

## The #array_unique() function only applies to one-dimensional arrays, not multi-dimensional arrays. However, you can use array_unique() to add values in a two-dimensional array.

The following example uses the array_unique() function to delete duplicate elements of a two-dimensional array. The specific code is as follows:,

$value){ $temp_array[$key] = array_unique($value); } $array = $temp_array; echo "
"; print_r($array); ?>

The output result is:

How to remove duplicate elements in PHP array

【Related tutorial recommendations】

1. Relevant topic recommendations: "

php array (Array)"

2. Related videos Course recommendation: "

Array statistical functions: count(), array_count_values() and array_unique()"


The above is the detailed content of How to remove duplicate elements in PHP 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