Home Backend Development PHP Problem 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

Apr 26, 2023 am 10:21 AM

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); //不同元素个数
Copy after login

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); //不同元素个数
Copy after login

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++;
    }
}
Copy after login

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); //不同元素个数
Copy after login

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24