In PHP programming, in_array() and array_count_values() are two very commonly used functions. in_array() is used to check whether an array contains a specific value, while array_count_values() is used to count the number of occurrences of each value in the array. In actual development, sometimes we need to combine these two functions to check whether a value exists in the array and count the number of times it appears. This article will explain how to use these two functions together.
The in_array() function is used to check whether a value exists in an array and returns a boolean value true or false , indicating whether the value is in the array.
$needle = 'apple';
$haystack = ['banana', 'apple', 'orange', 'apple'];
if (in_array($needle, $haystack)) {
echo "$needle Exist in an array!";
} else {
echo "$needle 不Exist in an array!";
}
The array_count_values() function returns an associative array, the key of the array is the only value in the array, and the value is the number of times these unique values appear in the original array.
$haystack = ['banana', 'apple', 'orange', 'apple', 'banana'];
$counted = array_count_values($haystack);
print_r($counted);
Output:
Array
(
[banana] => 2
[apple] => 2
[orange] => 1
)
We can use these two functions in combination to check whether a value exists in the array and count the number of times it appears in the array. Here is an example:
$haystack = ['banana', 'apple', 'orange', 'apple', 'banana'];
$needle = 'apple';
// use array_count_values() Get the number of occurrences of all values
$counted = array_count_values($haystack);
// use in_array() examine $needle 是否Exist in an array
if (in_array($needle, $haystack)) {
echo "$needle Exist in an array,And the number of occurrences is: " . $counted[$needle];
} else {
echo "$needle 不Exist in an array。";
}
In the above example, first we count the number of occurrences of each value in the array through the array_count_values() function, and then use the in_array() function to check whether the needle value exists. If it exists, output the number of occurrences of the value in the array.
For easy understanding, here is a complete example of how to use in_array() and array_count_values() to check whether a value exists and counts its occurrence:
<?php
// Define an array
$haystack = ['banana', 'apple', 'orange', 'apple', 'banana', 'apple'];
// 需要examine的值
$needle = 'apple';
// use array_count_values() Get the number of occurrences of each element in the array
$counted = array_count_values($haystack);
// examine是否存在并输出出现Second-rate数
if (in_array($needle, $haystack)) {
echo "$needle Exist in an array,It appears " . $counted[$needle] . " Second-rate。";
} else {
echo "$needle 不Exist in an array。";
}
?>
Execution results:
apple Exist in an array,It appears 3 Second-rate。
Combining the in_array() and array_count_values() functions, you can easily check whether a value exists in an array and count its occurrence. This is very useful in handling scenarios such as data analysis, log analysis, etc.
I hope that through this article, you can better understand how to use these two functions to simplify your code logic and improve development efficiency.