Home  >  Article  >  Backend Development  >  How to check if the values ​​in an array are repeated in php

How to check if the values ​​in an array are repeated in php

PHPz
PHPzOriginal
2023-04-24 09:07:241438browse

In PHP language, array is a very commonly used data type. When using arrays, we often need to check whether the values ​​in the array are repeated to avoid duplicate data. This article will introduce several ways to check whether the values ​​in an array are repeated in PHP.

Method 1: Use array_unique function

The array_unique function can be used to delete duplicate elements in an array and return a new array. If it is determined that the number of elements in the original array and the deduplicated array is the same, it means that the values ​​in the array are not repeated. The following is a sample code:

$arr = array(1, 2, 3, 3, 4);
$new_arr = array_unique($arr);
if (count($arr) == count($new_arr)) {
    echo "数组内的值没有重复";
} else {
    echo "数组内存在重复的值";
}

Method 2: Use the count and array_count_values ​​functions

The array_count_values ​​function can count the number of occurrences of each element in the array and return an associative array. If it is determined that the number of array elements after statistics is the same as the number of original arrays, it means that the values ​​in the array are not repeated. The code is as follows:

$arr = array(1, 2, 3, 3, 4);
$count_arr = array_count_values($arr);
if (count($arr) == count($count_arr)) {
    echo "数组内的值没有重复";
} else {
    echo "数组内存在重复的值";
}

Method 3: Use a loop to traverse the array

Use a loop to traverse the array and determine whether the elements in the array have appeared one by one. When an element appears more than once, it means that the values ​​in the array are repeated. Sample code:

$arr = array(1, 2, 3, 3, 4);
$repeat = false;
foreach ($arr as $value) {
    if (array_keys($arr, $value) !== array_last_key($arr, $value)) {
        $repeat = true;
        break;
    }
}
if (!$repeat) {
    echo "数组内的值没有重复";
} else {
    echo "数组内存在重复的值";
}

Among the three methods, the first method is the simplest and the most commonly used method. However, the second method and the third method are more suitable in certain scenarios, such as situations where complex operations on array elements are required. In actual use, you need to choose an appropriate method according to the specific situation to check whether the values ​​in the array are repeated.

In short, mastering these methods can improve the efficiency of PHP development and avoid unnecessary errors during the development process.

The above is the detailed content of How to check if the values ​​in an array are repeated in php. 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