During the development process, we often encounter the need to count the number of occurrences of an element in the array. Today, we will focus on how to use PHP statistics elements that only appear once in the array, and demonstrate the implementation with a simple example.
Suppose you have an array with multiple elements, and we want to find out the elements in this array that appear 1 times. For such needs, PHP provides multiple ways to achieve it, and today we will introduce an efficient and easy-to-understand way.
The built-in array_count_values function of PHP can count the occurrence times of all elements in an array. It will return an associative array, the key is the element in the array, and the value is the occurrence times of the corresponding element. By combining the array_filter function, we can easily filter out elements that only appear once.
<?php
// Define an array
$array = ['apple', 'banana', 'orange', 'apple', 'banana', 'grape', 'kiwi'];
// use array_count_values Function statistics number of elements occur
$counted = array_count_values($array);
// use array_filter The number of occurrences filtered is1Elements
$uniqueElements = array_filter($counted, function($count) {
return $count === 1;
});
// 获取只出现一次Elements的键
$uniqueKeys = array_keys($uniqueElements);
// Print results
echo "只出现一次Elements: " . implode(", ", $uniqueKeys);
?>
We define an array $array containing multiple elements.
Use array_count_values($array) to count the number of occurrences of each element in the array, and return an associative array $counted , for example: ['apple' => 2, 'banana' => 2, 'orange' => 1, 'grape' => 1, 'kiwi' => 1] .
Next, elements with 1 occurrences are filtered through array_filter($counted, function($count) { return $count === 1; }) .
Finally, use array_keys($uniqueElements) to get the keys of these elements that only appear once, and output the result in a string through the implode function.
只出现一次Elements: orange, grape, kiwi
In addition to using built-in functions, you can also directly iterate through the array to count the number of times the element appears. Here is a method to use foreach .
<?php
// Define an array
$array = ['apple', 'banana', 'orange', 'apple', 'banana', 'grape', 'kiwi'];
// Initialize an array of counters
$counter = [];
// Iterate through the array,Count the number of occurrences of each element
foreach ($array as $item) {
if (isset($counter[$item])) {
$counter[$item]++;
} else {
$counter[$item] = 1;
}
}
// The number of occurrences filtered is1Elements
$uniqueElements = [];
foreach ($counter as $key => $count) {
if ($count === 1) {
$uniqueElements[] = $key;
}
}
// Print results
echo "只出现一次Elements: " . implode(", ", $uniqueElements);
?>
We first initialize an empty $counter array to store the number of occurrences of each element.
Iterate through $array through foreach , and record the number of occurrences of each element in the $counter array.
Then, iterate over the $counter array again, filter out the elements with occurrences of 1 and store them in the $uniqueElements array.
Finally, the result is output through the implode function.
只出现一次Elements: orange, grape, kiwi
Both methods can effectively count elements that appear only once in the array. The first method is implemented through the built-in array_count_values and array_filter functions of PHP, and the code is concise and easy to understand. The second method is to manually traverse the array, which is suitable for some scenarios where performance requirements are high or do not rely on built-in functions.
Depending on your specific needs and preferences, you can choose a way that suits you to process the array data. Hope this article helps you!