What are the advantages of using array_keys to count occurrences in PHP?

When counting occurrences of values in an array in PHP, using array_keys can be advantageous as it allows you to easily get the unique values present in the array. This can then be used to count the occurrences of each unique value using array_count_values. This approach simplifies the counting process and provides a clear overview of the frequency of each value in the array.

// Sample array
$array = [1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5];

// Get unique values from the array
$unique_values = array_keys(array_count_values($array));

// Count occurrences of each unique value
foreach ($unique_values as $value) {
    $count = array_count_values($array)[$value];
    echo "Value: $value, Count: $count\n";
}