The array_count_values()
function in PHP is used to count the number of occurrences of each value in an array. This function has been consistent across PHP versions, including PHP 8.4, with no new changes introduced in this version.
Syntax
array_count_values(array $array): array
Parameters
$array
: The input array. Each value in the array is counted, and the keys of the resulting array are the unique values from the input.
Return Value
- Returns an associative array where the keys are the unique values from the input array and the values are the count of occurrences of each key.
Example 1: Basic Usage
<?php
$input = ["apple", "banana", "apple", "orange", "banana", "apple"];
$result = array_count_values($input);
print_r($result);
?>
Output:
Array
(
[apple] => 3
[banana] => 2
[orange] => 1
)
Example 2: Using Integer Values
<?php
$numbers = [1, 2, 3, 1, 2, 1];
$result = array_count_values($numbers);
print_r($result);
?>
Output:
Array
(
[1] => 3
[2] => 2
[3] => 1
)
PHP 8.4 Context
As of PHP 8.4, there are no updates or new features for the array_count_values()
function. Its behavior remains the same, ensuring backward compatibility.