How to determine if there are duplicate elements in an array in php

PHPz
Release: 2023-03-24 17:36:01
Original
1436 people have browsed it

PHP is a widely used scripting language that is widely used in web development. In PHP development, we often involve the data type of array. Determining whether there are duplicate elements in the array is also a very important task. In this article, we will discuss how to determine if there are duplicate elements in an array using PHP.

Use the array_count_values() function for statistics

Another method is to use the array_count_values() function for statistics. This function will return a new array, where Contains the number of occurrences of each element. We can iterate through the new array and determine elements that appear more than 1 as duplicate elements.

The following is a sample code:

<?php

$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;orange&#39;, &#39;orange&#39;);
$arr_new = array_count_values($arr);

foreach($arr_new as $key => $value) {
    if($value != 1) {
        unset($arr[$key]);
    }
}

print_r($arr);
?>
Copy after login

The output result is:

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

The method of using the array_count_values() function for statistics is also very good and has certain practicality .

Expand knowledge: remove duplicates

  • Use the array_unique() function to remove duplicate elements

array_unique() function is a function that has been encapsulated in PHP to remove duplicate elements from an array. Its syntax structure is as follows:

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

This function will return a new array after removing duplicate elements, in which sort_flags is an optional parameter used to set the sorting rules. By default, the function performs a string comparison of all values ​​and sorts them in ascending order. We can select the parameters to sort according to our needs.

The following is an example:

<?php

$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;orange&#39;, &#39;orange&#39;);
$arr_new = array_unique($arr);
print_r($arr_new);
?>
Copy after login

The output result is:

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

As you can see, the function successfully removes duplicate elements from the array.

  • Using loops to remove duplicates

In PHP development, we can use loops to traverse arrays, and Perform deduplication operations in a timely manner. The specific steps are as follows:

1. Get the length of the array
We can use the count() function to get the length of the array.

2. Loop through the array
Use the for loop statement to traverse the array. At this time, you can use two variables i and j, where i is used to point to the current element being operated on, and j is used to point to the next bit. element.

3. Determine whether there are duplicate elements
Inside the loop, we can use the if statement to judge. If the i element is equal to the j element, it is considered that there is a duplicate element, and the unset() function can be used to delete the element. element.

The following is a sample code:

<?php

$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;orange&#39;, &#39;orange&#39;);
$len = count($arr);

for($i=0; $i<$len-1; $i++) {
    for($j=$i+1; $j<$len; $j++) {
        if($arr[$i] == $arr[$j]) {
            unset($arr[$j]);
        }
    }
}

print_r($arr);
?>
Copy after login

The output result is:

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

As you can see, the effect of deduplication through loops is also quite good. of

The above is the detailed content of How to determine if there are duplicate elements in an array in php. 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