array_count_values is a very practical function in PHP that counts the number of occurrences of all values in an array. This function returns an associative array where the keys of the array are the values in the original array, and the values of the array are the number of times these values appear in the original array.
However, sometimes when using array_count_values , you may find that boolean values ( true and false ) and NULL do not appear in the statistics. Why is this? Let’s analyze it today.
First, let’s review the basic usage of the array_count_values function. This function takes an array as an argument and returns a new array, counting the number of occurrences of each element in the array. For example:
$arr = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana'];
$result = array_count_values($arr);
print_r($result);
The output will be:
Array
(
[apple] => 2
[banana] => 3
[orange] => 1
)
Next, we analyze why Boolean and NULL are ignored.
In PHP, the boolean values true and false are converted to integer values 1 and 0 , and in the array_count_values function, the boolean values are considered to be the same value. Specifically, true and false are treated as the same 1 and 0 , and array_count_values merge them when processing arrays. Therefore, if there are multiple boolean values in the array, their occurrences will be merged into the corresponding integer value instead of counting the boolean values separately.
$arr = [true, false, true, 1, 0];
$result = array_count_values($arr);
print_r($result);
The output will be:
Array
(
[1] => 3
[0] => 2
)
You can see that true and 1 are merged into 1 , while false and 0 are merged into 0 , so the boolean value is not counted separately.
For NULL values, PHP also has a special behavior when processing arrays. When array_count_values is used, NULL is ignored because it is usually considered "null" in PHP. If you need to count the number of occurrences of NULL , you can consider converting it to another value first, or processing it in other ways.
For example, consider the following code:
$arr = [NULL, 'apple', NULL, 'banana', 'apple'];
$result = array_count_values($arr);
print_r($result);
The output will be:
Array
(
[apple] => 2
[banana] => 1
)
As you can see, NULL does not appear in the result. To solve this problem, you can replace all NULL values with a specific value, such as 'NULL' before calling array_count_values , and then perform statistics.
$arr = [NULL, 'apple', NULL, 'banana', 'apple'];
$arr = array_map(function($value) {
return $value === NULL ? 'NULL' : $value;
}, $arr);
$result = array_count_values($arr);
print_r($result);
The output will be:
Array
(
[NULL] => 2
[apple] => 2
[banana] => 1
)
This way, you can include NULL values when stating.
array_count_values is a very convenient function to count the number of occurrences of each value in an array. However, because Boolean and NULL values in PHP have special conversion and ignorance behaviors when processing, they are not counted separately. If you need to deal with these special cases, you can manually replace these values before calling array_count_values , or type conversion to ensure that they can be counted correctly.
Final content: