Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished | checked

PHP array_count_values function

It counts the number of times a value appears in the provided array and returns an associative array where the keys are the values from the provided array, and the values are the number of times the value appears in the provided array.

Description / Descripción
array_count_values(array $array): array
Sintaxis / Sintax
$quantity = array_count_values($array_proporcionado);

Parameters

$array – the array provided

Return

It returns an associative array with keys that are the values from the provided array, and values that are the number of times each value appears.

You might be wondering what happens if you pass an associative array with multiple values. In this case, you should specify the column with array_column beforehand so that you have an array that you can process with array_count_values.

Example

This is a simple example where we will see how many times each name repeats.

$array = array("Julio", "Rodrigo", "Juan", "Pedro", "Paolo","Rodrigo","Rodrigo","Juan","Antonio","Nahuel");
print_r(array_count_values($array));
Array
(
    [Julio] => 1
    [Rodrigo] => 3
    [Juan] => 2
    [Pedro] => 1
    [Paolo] => 1
    [Antonio] => 1
    [Nahuel] => 1
)
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments