Count array values in php

❮ 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

ParameterDescription
array Required. Specifying the array to count values of

Technical Details

Return Value:PHP Version:
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

ParameterDescription
array Required. Specifies the array
mode Optional. Specifies the mode. Possible values:
  • 0 - Default. Does not count all elements of multidimensional arrays
  • 1 - Counts the array recursively [counts all the elements of multidimensional arrays]

Technical Details

Return Value:PHP Version:PHP Changelog:
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_valuesCounts 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!".

How do you count numbers in an array?

Approach used in the below program is as follows.
Input an array let's say, int arr[].
Calculate the length of both the arrays using the length[] function that will return an integer value as per the elements in an array..
Start the loop from i to 0 till i less than size of an array..

How do I count the number of repeated numbers in an array in PHP?

array_count_values[] function in PHP The array_count_values[] function returns an array with the number of occurrences for each value. It returns an associative array. The returned array has keys as the array's values, whereas values as the count of the passed values.

What is the use of Array_count_values [] in PHP explain with example?

The array_count_values[] function is used to count all the values inside an array. In other words, we can say that array_count_values[] function is used to calculate the frequency of all of the elements of an array. Parameters: This function accepts a single parameter $array.

What is the use of count function in PHP?

The count[] function counts elements in an array, or properties in an object. It returns the number of elements in an array.

Chủ Đề