Home > Backend Development > PHP Problem > A brief analysis of several methods of obtaining unique values ​​in an array in PHP

A brief analysis of several methods of obtaining unique values ​​in an array in PHP

PHPz
Release: 2023-04-12 11:30:44
Original
854 people have browsed it

In PHP programming, arrays can be used to conveniently store and process a set of data. However, sometimes we need to get unique values ​​from an array. This article will introduce several methods to obtain unique values ​​in an array.

Method 1: Use the array_unique() function

The array_unique() function in PHP can remove duplicate values ​​from the array and return a new array composed of non-duplicate values.

Sample code:

$array = array("apple", "banana", "apple", "orange", "banana");
$new_array = array_unique($array);
print_r($new_array);
Copy after login

Output:

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

Method 2: Use the array_count_values() function

The array_count_values() function in PHP can count the array The number of occurrences of each value, and returns a new array where the key is the value of the original array and the value is the number of times the value appears in the original array.

We can use this function to further filter out values ​​that appear only once in the original array. Sample code:

$array = array("apple", "banana", "apple", "orange", "banana");
$count_array = array_count_values($array);
$new_array = array();
foreach ($count_array as $key => $value) {
    if ($value == 1) {
        array_push($new_array, $key);
    }
}
print_r($new_array);
Copy after login

Output:

Array
(
    [0] => orange
)
Copy after login

Method 3: Use array_keys() function

The array_keys() function in PHP can return a new key value that contains all the key values ​​​​of the array array. If there are duplicate values ​​in the original array, the keys of the duplicate values ​​will only be retained once.

We can use this function to get all the keys in the original array, and then get the corresponding values ​​based on these keys. Sample code:

$array = array("apple", "banana", "apple", "orange", "banana");
$key_array = array_keys($array);
$new_array = array();
foreach ($key_array as $key) {
    $value = $array[$key];
    if (array_keys($new_array, $value) == false) {
        array_push($new_array, $value);
    }
}
print_r($new_array);
Copy after login

Output:

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

Method 4: Use array_diff() function

The array_diff() function in PHP can calculate the difference between two or more arrays set and returns a new array consisting of values ​​that exist in the first array but not in the other arrays.

We can perform a difference operation on the original array and an empty array, and the resulting new array will be the non-repeating values ​​in the original array. Sample code:

$array = array("apple", "banana", "apple", "orange", "banana");
$new_array = array_diff($array, array());
print_r($new_array);
Copy after login

Output:

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

To sum up, we can use a variety of methods to obtain unique values ​​in a PHP array. Choosing the appropriate method according to the actual situation can improve the efficiency of the program.

The above is the detailed content of A brief analysis of several methods of obtaining unique values ​​in an array 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