CKTIS / PHP
array_count_values()
Build a frequency table from integer or string values.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
array_count_values(array $array): arrayBuild a frequency table from integer or string values. Each distinct value becomes an array key and its count becomes the corresponding value. PHP array key conversion can merge an integer and its decimal string representation. Unsupported element types produce warnings and contribute no count.
Signature
array_count_values(array $array): array
Example output
{"4":2,"04":1,"blue":2}
Examples
<?php
$bins = [4, '4', '04', 'blue', 'blue'];
echo json_encode(array_count_values($bins));
Result
{"4":2,"04":1,"blue":2}