Home  >  Article  >  Backend Development  >  How to get the number of different elements in an array in php

How to get the number of different elements in an array in php

PHPz
PHPzOriginal
2023-04-26 10:21:54629browse

In PHP development, operating arrays is a very common operation. Among them, getting the number of different elements in an array is also very practical. This article will introduce several methods to obtain the number of different elements in an array.

1. Use the array_count_values ​​function

The array_count_values ​​function is a function in PHP used to count the number of occurrences of all values ​​in an array. Its return value is an associative array, its key value is the element value in the array, and its corresponding element value is the number of times the element value appears in the original array. Therefore, we can use the return value of this function to get the number of different elements in the array.

The example is as follows:

$arr = array(1, 2, 2, 3, 3, 3);
$values = array_count_values($arr);
$count = count($values); //不同元素个数

The output result of the above code is: 3, that is, the number of different elements in the array is 3.

2. Use the array_unique function

The array_unique function is a function in PHP used to remove duplicate elements from an array. Its return value is the array after removing duplicate elements. Therefore, we can get the deduplicated array by first calling this function, and then calling the count function to get the number of different elements.

The example is as follows:

$arr = array(1, 2, 2, 3, 3, 3);
$unique_arr = array_unique($arr);
$count = count($unique_arr); //不同元素个数

The output result of the above code is: 3, that is, the number of different elements in the array is 3.

3. Using a loop to traverse an array

Looping through an array is also a way to get the number of different elements in the array. We can define an array to record the elements that have been visited, and when traversing the array, determine whether the current element has been visited, if so, skip it, otherwise add it to the visited array, and add 1 to the different element counter .

The example is as follows:

$arr = array(1, 2, 2, 3, 3, 3);
$visited_arr = array();
$count = 0; //不同元素个数计数器
foreach ($arr as $value) {
    if (!in_array($value, $visited_arr)) { // 判断当前元素是否已经被访问过
        $visited_arr[] = $value;
        $count++;
    }
}

The output result of the above code is: 3, that is, the number of different elements in the array is 3.

4. Use the array_diff function

The array_diff function is a function in PHP that calculates the array difference. Therefore, we can use this function to get the number of different elements in the array. The specific method is: first deduplicate the original array, then perform a difference set with the original array to remove duplicate elements, and then call the count function to obtain the number of different elements.

The example is as follows:

$arr = array(1, 2, 2, 3, 3, 3);
$unique_arr = array_unique($arr);
$diff_arr = array_diff($arr, $unique_arr);
$count = count($diff_arr); //不同元素个数

The output result of the above code is: 3, that is, the number of different elements in the array is 3.

To sum up, we have introduced several methods to obtain the number of different elements in an array. In actual development, the most suitable method can be selected according to the specific situation.

The above is the detailed content of How to get the number of different elements in an array 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