❮ PHP Array Reference
Example
Count all the values of an array:
Try it Yourself »
Definition and Usage
The array_count_values[] function counts all the values of an array.
Syntax
array_count_values[array]
Parameter Values
array | Required. Specifying the array to count values of |
Technical Details
Returns an associative array, where the keys are the original array's values, and the values are the number of occurrences |
4+ |
❮ PHP Array Reference
❮ PHP Array Reference
Example
Return the number of elements in an array:
Try it Yourself »
Definition and Usage
The count[] function returns the number of elements in an array.
Syntax
Parameter Values
array | Required. Specifies the array |
mode | Optional. Specifies the mode. Possible values:
|
Technical Details
Returns the number of elements in the array |
4+ |
The mode parameter was added in PHP 4.2 |
More Examples
Example
Count the array recursively:
Try it Yourself »
❮ PHP Array Reference
[PHP 4, PHP 5, PHP 7, PHP 8]
array_count_values — Counts all the values of an array
Description
array_count_values[array $array
]: array
Parameters
array
The array of values to count
Return Values
Returns an associative array of values from array
as keys and their count as value.
Errors/Exceptions
Throws E_WARNING
for every element which is not string or int.
Examples
Example #1 array_count_values[] example
The above example will output:
Array [ [1] => 2 [hello] => 2 [world] => 1 ]
See Also
- count[] - Counts all elements in an array or in a Countable object
- array_unique[] - Removes duplicate values from an array
- array_values[] - Return all the values of an array
- count_chars[] - Return information about characters used in a string
sergolucky96 at gmail dot com ¶
4 years ago
Simple way to find number of items with specific values in multidimensional array:
szczepan.krolgmail.c0m ¶
12 years ago
Here is a Version with one or more arrays, which have similar values in it:
Use $lower=true/false to ignore/set case Sensitiv.
anvil_sa at NOSPAMNO dot hotmail dot com ¶
2 years ago
Based on sergolucky96 suggestion
Simple way to find number of items with specific *boolean* values in multidimensional array:
Dominic Vonk ¶
8 years ago
The case-insensitive version:
rabies dot dostojevski at gmail dot com ¶
15 years ago
I couldn't find a function for counting the values with case-insensitive matching, so I wrote a quick and dirty solution myself:
This prints:
Array
[
[J. Karjalainen] => 3
[60] => 2
[j. karjalainen] => 1
[Fastway] => 2
[FASTWAY] => 1
[fastway] => 1
[YUP] => 1
]
Array
[
[J. Karjalainen] => 4
[60] => 2
[Fastway] => 4
[YUP] => 1
]
I don't know how efficient it is, but it seems to work. Needed this function in one of my scripts and thought I would share it.
pmarcIatIgeneticsImedIharvardIedu ¶
19 years ago
array_count_values function does not work on multidimentional arrays.
If $score[][] is a bidimentional array, the command
"array_count_values [$score]" return the error message "Warning: Can only count STRING and INTEGER values!".