Home > Backend Development > PHP Problem > How to determine if arrays have the same value in php

How to determine if arrays have the same value in php

PHPz
Release: 2023-04-26 11:22:26
Original
974 people have browsed it

In PHP, there are many ways to determine whether an array has the same value. Let's introduce these methods separately.

Method 1: Use loop traversal

Using a loop to traverse the array is the simplest way. The code is as follows:

function hasDuplicate($arr) {
    $len = count($arr);
    for ($i = 0; $i < $len; $i++) {
        for ($j = $i+1; $j < $len; $j++) {
            if ($arr[$i] == $arr[$j]) {
                return true;
            }
        }
    }
    return false;
}
Copy after login

This method uses a double loop, first traversing the array Each element in the element is compared with each element after it. If there are the same elements, it returns true, otherwise it returns false.

Method 2: Use PHP built-in function

PHP built-in functionarray_unique() You can remove duplicate values ​​​​in the array. By comparing the length of the array, you can determine the original array Whether there are duplicate values ​​in the array, the code is as follows:

function hasDuplicate($arr) {
    $unique_arr = array_unique($arr);
    return count($arr) != count($unique_arr);
}
Copy after login

This method first uses the array_unique() function to remove duplicate values, and then compares whether the lengths of the original array and the deduplicated array are equal. If If not equal, it means there are duplicate values ​​in the original array.

Method 3: Use PHP built-in function

PHP built-in functionarray_count_values() can return the number of occurrences of all elements in an array, by comparing the number of occurrences of each value in the array If the number of occurrences exceeds 1, you can determine whether there are duplicate values ​​in the original array. The code is as follows:

function hasDuplicate($arr) {
    $counts = array_count_values($arr);
    foreach ($counts as $count) {
        if ($count > 1) {
            return true;
        }
    }
    return false;
}
Copy after login

This method first uses the array_count_values() function to get the number of occurrences of each element, and then Traverse the number of occurrences of each element. If an element appears more than 1, it means there are duplicate values ​​in the original array.

Method 4: Use PHP built-in function

PHP built-in functionarray_diff_assoc() You can compare whether the key values ​​​​of two arrays are the same by comparing the original array and the deduplicated array By using the difference set of the array, you can determine whether there are duplicate values ​​in the original array. The code is as follows:

function hasDuplicate($arr) {
    $unique_arr = array_unique($arr);
    $diff_arr = array_diff_assoc($arr, $unique_arr);
    return !empty($diff_arr);
}
Copy after login

This method first uses the array_unique() function to remove duplicate values, and then uses array_diff_assoc() The function compares two arrays and finally determines whether the difference set is empty. If it is not empty, it means there are duplicate values ​​in the original array.

To sum up, the above four methods can all be used to determine whether an array has the same value. Each has its own advantages and disadvantages. You can choose the appropriate method according to the actual situation.

The above is the detailed content of How to determine if arrays have the same value in php. For more information, please follow other related articles on the PHP Chinese website!

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